Skip to content

Commit

Permalink
Bind godot.instance_from_id
Browse files Browse the repository at this point in the history
  • Loading branch information
Geequlim committed Aug 7, 2020
1 parent 755381c commit 2806a8f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
4 changes: 4 additions & 0 deletions misc/godot.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@ declare module godot {
* ```*/
function load(path: string): Resource;

/**
* Returns the Object that corresponds to `instance_id`. All Objects have a unique instance ID.
*/
function instance_from_id(instance_id: number): Object;

/**
* Wait a signal of an object
Expand Down
12 changes: 12 additions & 0 deletions quickjs/quickjs_binder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,15 @@ JSValue QuickJSBinder::godot_load(JSContext *ctx, JSValue this_val, int argc, JS
return variant_to_var(ctx, res);
}

JSValue QuickJSBinder::godot_instance_from_id(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) {
#ifdef DEBUG_METHODS_ENABLED
ERR_FAIL_COND_V(argc < 1 || !JS_IsNumber(argv[0]), JS_ThrowTypeError(ctx, "number expected for %s.%s", GODOT_OBJECT_NAME, "instance_from_id"));
#endif
ObjectID id = js_to_uint64(ctx, argv[0]);
Object *obj = ObjectDB::get_instance(id);
return variant_to_var(ctx, obj);
}

void QuickJSBinder::add_debug_binding_info(JSContext *ctx, JSValueConst p_obj, const ECMAScriptGCHandler *p_bind) {
if (!p_bind) return;
JSValue classname = JS_UNDEFINED;
Expand Down Expand Up @@ -1161,6 +1170,9 @@ void QuickJSBinder::add_godot_globals() {
// godot.load
JSValue js_func_load = JS_NewCFunction(ctx, godot_load, "load", 1);
JS_DefinePropertyValueStr(ctx, godot_object, "load", js_func_load, PROP_DEF_DEFAULT);
// godot.instance_from_id
JSValue js_godot_instance_from_id = JS_NewCFunction(ctx, godot_instance_from_id, "instance_from_id", 1);
JS_DefinePropertyValueStr(ctx, godot_object, "instance_from_id", js_godot_instance_from_id, PROP_DEF_DEFAULT);
{
// godot.DEBUG_ENABLED
#ifdef DEBUG_ENABLED
Expand Down
12 changes: 12 additions & 0 deletions quickjs/quickjs_binder.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ class QuickJSBinder : public ECMAScriptBinder {
static JSValue godot_to_string(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv);
static JSValue godot_get_type(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv);
static JSValue godot_load(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv);
static JSValue godot_instance_from_id(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv);

static void add_debug_binding_info(JSContext *ctx, JSValueConst p_obj, const ECMAScriptGCHandler *p_bind);

const ECMAClassInfo *register_ecma_class(const JSValueConst &p_constructor, const String &p_path);
Expand Down Expand Up @@ -251,6 +253,16 @@ class QuickJSBinder : public ECMAScriptBinder {
JS_ToUint32(ctx, &u, p_val);
return u;
}
_FORCE_INLINE_ static int64_t js_to_int64(JSContext *ctx, const JSValueConst &p_val) {
int64_t i;
JS_ToInt64(ctx, &i, p_val);
return i;
}
_FORCE_INLINE_ static uint64_t js_to_uint64(JSContext *ctx, const JSValueConst &p_val) {
uint64_t i;
JS_ToIndex(ctx, &i, p_val);
return i;
}
_FORCE_INLINE_ static JSValue to_js_number(JSContext *ctx, real_t p_val) {
return JS_NewFloat64(ctx, double(p_val));
}
Expand Down

0 comments on commit 2806a8f

Please sign in to comment.