Skip to content

Commit

Permalink
Merge pull request #17980 from karroffel/nativescript-1.1-global-type…
Browse files Browse the repository at this point in the history
…-tags

[NativeScript] added global type tag system
  • Loading branch information
karroffel authored Apr 5, 2018
2 parents 43f298a + ad93d3e commit eaa5dfb
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 24 deletions.
17 changes: 17 additions & 0 deletions modules/gdnative/gdnative_api.json
Original file line number Diff line number Diff line change
Expand Up @@ -5821,6 +5821,23 @@
["godot_string", "p_documentation"]
]
},
{
"name": "godot_nativescript_set_global_type_tag",
"return_type": "void",
"arguments": [
["int", "p_idx"],
["const char *", "p_name"],
["const void *", "p_type_tag"]
]
},
{
"name": "godot_nativescript_get_global_type_tag",
"return_type": "const void *",
"arguments": [
["int", "p_idx"],
["const char *", "p_name"]
]
},
{
"name": "godot_nativescript_set_type_tag",
"return_type": "void",
Expand Down
5 changes: 4 additions & 1 deletion modules/gdnative/include/nativescript/godot_nativescript.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,16 @@ void GDAPI godot_nativescript_set_signal_documentation(void *p_gdnative_handle,

// type tag API

void GDAPI godot_nativescript_set_global_type_tag(int p_idx, const char *p_name, const void *p_type_tag);
const void GDAPI *godot_nativescript_get_global_type_tag(int p_idx, const char *p_name);

void GDAPI godot_nativescript_set_type_tag(void *p_gdnative_handle, const char *p_name, const void *p_type_tag);
const void GDAPI *godot_nativescript_get_type_tag(const godot_object *p_object);

// instance binding API

typedef struct {
GDCALLINGCONV void *(*alloc_instance_binding_data)(void *, godot_object *);
GDCALLINGCONV void *(*alloc_instance_binding_data)(void *, const void *, godot_object *);
GDCALLINGCONV void (*free_instance_binding_data)(void *, void *);
void *data;
GDCALLINGCONV void (*free_func)(void *);
Expand Down
16 changes: 12 additions & 4 deletions modules/gdnative/nativescript/godot_nativescript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,14 @@ void GDAPI godot_nativescript_set_signal_documentation(void *p_gdnative_handle,
signal->get().documentation = *(String *)&p_documentation;
}

void GDAPI godot_nativescript_set_global_type_tag(int p_idx, const char *p_name, const void *p_type_tag) {
NativeScriptLanguage::get_singleton()->set_global_type_tag(p_idx, StringName(p_name), p_type_tag);
}

const void GDAPI *godot_nativescript_get_global_type_tag(int p_idx, const char *p_name) {
return NativeScriptLanguage::get_singleton()->get_global_type_tag(p_idx, StringName(p_name));
}

void GDAPI godot_nativescript_set_type_tag(void *p_gdnative_handle, const char *p_name, const void *p_type_tag) {
String *s = (String *)p_gdnative_handle;

Expand Down Expand Up @@ -347,10 +355,6 @@ const void GDAPI *godot_nativescript_get_type_tag(const godot_object *p_object)
return NULL;
}

#ifdef __cplusplus
}
#endif

int GDAPI godot_nativescript_register_instance_binding_data_functions(godot_instance_binding_functions p_binding_functions) {
return NativeScriptLanguage::get_singleton()->register_binding_functions(p_binding_functions);
}
Expand All @@ -362,3 +366,7 @@ void GDAPI godot_nativescript_unregister_instance_binding_data_functions(int p_i
void GDAPI *godot_nativescript_get_instance_binding_data(int p_idx, godot_object *p_object) {
return NativeScriptLanguage::get_singleton()->get_instance_binding_data(p_idx, (Object *)p_object);
}

#ifdef __cplusplus
}
#endif
44 changes: 25 additions & 19 deletions modules/gdnative/nativescript/nativescript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,6 @@
#include "editor/editor_node.h"
#endif

//
//
// Script stuff
//
//

void NativeScript::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_class_name", "class_name"), &NativeScript::set_class_name);
ClassDB::bind_method(D_METHOD("get_class_name"), &NativeScript::get_class_name);
Expand Down Expand Up @@ -528,12 +522,6 @@ NativeScript::~NativeScript() {
#endif
}

//
//
// ScriptInstance stuff
//
//

#define GET_SCRIPT_DESC() script->get_script_desc()

void NativeScriptInstance::_ml_call_reversed(NativeScriptDesc *script_data, const StringName &p_method, const Variant **p_args, int p_argcount) {
Expand Down Expand Up @@ -872,12 +860,6 @@ NativeScriptInstance::~NativeScriptInstance() {
}
}

//
//
// ScriptingLanguage stuff
//
//

NativeScriptLanguage *NativeScriptLanguage::singleton;

void NativeScriptLanguage::_unload_stuff(bool p_reload) {
Expand Down Expand Up @@ -1195,8 +1177,11 @@ void *NativeScriptLanguage::get_instance_binding_data(int p_idx, Object *p_objec
}

if (!(*binding_data)[p_idx]) {

const void *global_type_tag = global_type_tags[p_idx].get(p_object->get_class_name());

// no binding data yet, soooooo alloc new one \o/
(*binding_data)[p_idx] = binding_functions[p_idx].second.alloc_instance_binding_data(binding_functions[p_idx].second.data, (godot_object *)p_object);
(*binding_data)[p_idx] = binding_functions[p_idx].second.alloc_instance_binding_data(binding_functions[p_idx].second.data, global_type_tag, (godot_object *)p_object);
}

return (*binding_data)[p_idx];
Expand Down Expand Up @@ -1238,6 +1223,27 @@ void NativeScriptLanguage::free_instance_binding_data(void *p_data) {
delete &binding_data;
}

void NativeScriptLanguage::set_global_type_tag(int p_idx, StringName p_class_name, const void *p_type_tag) {
if (!global_type_tags.has(p_idx)) {
global_type_tags.insert(p_idx, HashMap<StringName, const void *>());
}

HashMap<StringName, const void *> &tags = global_type_tags[p_idx];

tags.set(p_class_name, p_type_tag);
}

const void *NativeScriptLanguage::get_global_type_tag(int p_idx, StringName p_class_name) const {
if (!global_type_tags.has(p_idx))
return NULL;

const HashMap<StringName, const void *> &tags = global_type_tags[p_idx];

const void *tag = tags.get(p_class_name);

return tag;
}

#ifndef NO_THREADS
void NativeScriptLanguage::defer_init_library(Ref<GDNativeLibrary> lib, NativeScript *script) {
MutexLock lock(mutex);
Expand Down
6 changes: 6 additions & 0 deletions modules/gdnative/nativescript/nativescript.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "core/self_list.h"
#include "io/resource_loader.h"
#include "io/resource_saver.h"
#include "oa_hash_map.h"
#include "ordered_hash_map.h"
#include "os/thread_safe.h"
#include "scene/main/node.h"
Expand Down Expand Up @@ -240,6 +241,8 @@ class NativeScriptLanguage : public ScriptLanguage {
Vector<Pair<bool, godot_instance_binding_functions> > binding_functions;
Set<Vector<void *> *> binding_instances;

Map<int, HashMap<StringName, const void *> > global_type_tags;

public:
// These two maps must only be touched on the main thread
Map<String, Map<StringName, NativeScriptDesc> > library_classes;
Expand Down Expand Up @@ -323,6 +326,9 @@ class NativeScriptLanguage : public ScriptLanguage {

virtual void *alloc_instance_binding_data(Object *p_object);
virtual void free_instance_binding_data(void *p_data);

void set_global_type_tag(int p_idx, StringName p_class_name, const void *p_type_tag);
const void *get_global_type_tag(int p_idx, StringName p_class_name) const;
};

inline NativeScriptDesc *NativeScript::get_script_desc() const {
Expand Down

0 comments on commit eaa5dfb

Please sign in to comment.