Skip to content

Commit 706d39a

Browse files
committed
Add tooled and icon support of ecmascript
Update typescript api documentation
1 parent 8d0262e commit 706d39a

File tree

3 files changed

+55
-4
lines changed

3 files changed

+55
-4
lines changed

misc/godot.builtin.d.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,37 @@
11
declare module godot {
2-
2+
3+
type GodotClass = new() => godot.Object;
4+
35
/**
46
* Export class to godot
57
*
68
* @param target The class extends from `godot.Object`
79
* @param name The class name
810
*/
9-
function register_class(target: Function, name: string);
11+
function register_class(target: GodotClass, name: string);
1012

1113
/**
1214
* Register signal to class
1315
* @param target The class of the signal
1416
* @param name signal name
1517
*/
16-
function register_signal(target: Function|object, name: string);
18+
function register_signal(target: GodotClass | godot.Object, name: string);
1719

1820
/**
1921
* Register property to class
2022
* @param target The class of the property
2123
* @param name The name of the property
2224
* @param value The default value of the property
2325
*/
24-
function register_property(target: Function|object, name: string, value: any);
26+
function register_property(target: GodotClass | godot.Object, name: string, value: any);
27+
28+
/**
29+
* The meta data of an script
30+
* @param target The script class
31+
* @param tool is tooled of this class
32+
* @param icon The icon of the class
33+
*/
34+
function set_script_meta(target: GodotClass, tool: boolean, icon?: string);
2535

2636
/**
2737
* Returns the internal type of the given `Variant` object, using the `godot.TYPE_*`
@@ -38,6 +48,14 @@ declare module godot {
3848
* ```*/
3949
function load(path: string): Resource;
4050

51+
52+
/**
53+
* Wait a signal of an object
54+
* @param target The owner of the signal to wait
55+
* @param signal The signal to wait
56+
*/
57+
function yield(target: godot.Object, signal: string): Promise<any[]>;
58+
4159
const E: 2.7182818284590452353602874714;
4260
const LN2: 0.6931471805599453094172321215;
4361
const SQRT2: 1.4142135623730950488016887242;

quickjs/quickjs_binder.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,9 @@ void QuickJSBinder::add_godot_globals() {
755755
// godot.register_property
756756
JSValue js_func_register_property = JS_NewCFunction(ctx, godot_register_property, "register_property", 3);
757757
JS_DefinePropertyValueStr(ctx, godot_object, "register_property", js_func_register_property, PROP_DEF_DEFAULT);
758+
// godot.set_script_meta
759+
JSValue js_func_set_script_meta = JS_NewCFunction(ctx, godot_set_script_metadata, "set_script_meta", 3);
760+
JS_DefinePropertyValueStr(ctx, godot_object, "set_script_meta", js_func_set_script_meta, PROP_DEF_DEFAULT);
758761
// godot.get_type
759762
JSValue js_get_type = JS_NewCFunction(ctx, godot_get_type, "get_type", 1);
760763
JS_DefinePropertyValueStr(ctx, godot_object, "get_type", js_get_type, PROP_DEF_DEFAULT);
@@ -805,6 +808,8 @@ void QuickJSBinder::initialize() {
805808
js_key_godot_classid = JS_NewAtom(ctx, JS_HIDDEN_SYMBOL("cls"));
806809
js_key_godot_exports = JS_NewAtom(ctx, JS_HIDDEN_SYMBOL("exports"));
807810
js_key_godot_signals = JS_NewAtom(ctx, JS_HIDDEN_SYMBOL("signals"));
811+
js_key_godot_tooled = JS_NewAtom(ctx, JS_HIDDEN_SYMBOL("tool"));
812+
js_key_godot_icon_path = JS_NewAtom(ctx, JS_HIDDEN_SYMBOL("icon"));
808813
JS_DefinePropertyValueStr(ctx, global_object, GODOT_OBJECT_NAME, godot_object, PROP_DEF_DEFAULT);
809814
// godot.GodotOrigin
810815
add_godot_origin();
@@ -856,6 +861,8 @@ void QuickJSBinder::uninitialize() {
856861
module_cache.clear();
857862

858863
JS_FreeAtom(ctx, js_key_godot_classid);
864+
JS_FreeAtom(ctx, js_key_godot_tooled);
865+
JS_FreeAtom(ctx, js_key_godot_icon_path);
859866
JS_FreeAtom(ctx, js_key_godot_exports);
860867
JS_FreeAtom(ctx, js_key_godot_signals);
861868
JS_FreeValue(ctx, empty_function);
@@ -1072,6 +1079,8 @@ const ECMAClassInfo *QuickJSBinder::register_ecma_class(const JSValue &p_constru
10721079
QuickJSBinder *binder = get_context_binder(ctx);
10731080
JSValue prototype = JS_UNDEFINED;
10741081
JSValue classid = JS_UNDEFINED;
1082+
JSValue tooled = JS_UNDEFINED;
1083+
JSValue icon = JS_UNDEFINED;
10751084
JSClassID id = 0;
10761085

10771086
if (!JS_IsFunction(ctx, p_constructor)) {
@@ -1081,6 +1090,9 @@ const ECMAClassInfo *QuickJSBinder::register_ecma_class(const JSValue &p_constru
10811090

10821091
prototype = JS_GetProperty(ctx, p_constructor, QuickJSBinder::JS_ATOM_prototype);
10831092
classid = JS_GetProperty(ctx, prototype, js_key_godot_classid);
1093+
tooled = JS_GetProperty(ctx, p_constructor, js_key_godot_tooled);
1094+
icon = JS_GetProperty(ctx, p_constructor, js_key_godot_icon_path);
1095+
10841096
if (JS_IsUndefined(classid)) {
10851097
JS_ThrowTypeError(ctx, "ECMAClass class expected: %s", p_path.utf8().ptr());
10861098
goto fail;
@@ -1102,6 +1114,10 @@ const ECMAClassInfo *QuickJSBinder::register_ecma_class(const JSValue &p_constru
11021114
ecma_class.class_name = class_name;
11031115
ecma_class.prototype.ecma_object = JS_VALUE_GET_PTR(prototype);
11041116
ecma_class.constructor.ecma_object = JS_VALUE_GET_PTR(p_constructor);
1117+
ecma_class.tool = JS_ToBool(ctx, tooled);
1118+
if (JS_IsString(icon)) {
1119+
ecma_class.icon_path = js_to_string(ctx, icon);
1120+
}
11051121

11061122
// signals
11071123
JSValue signals = JS_GetProperty(ctx, prototype, js_key_godot_signals);
@@ -1149,6 +1165,8 @@ const ECMAClassInfo *QuickJSBinder::register_ecma_class(const JSValue &p_constru
11491165
fail:
11501166
JS_FreeValue(ctx, classid);
11511167
JS_FreeValue(ctx, prototype);
1168+
JS_FreeValue(ctx, icon);
1169+
JS_FreeValue(ctx, tooled);
11521170
return binder->ecma_classes.getptr(p_path);
11531171
}
11541172

@@ -1213,6 +1231,18 @@ JSValue QuickJSBinder::godot_register_property(JSContext *ctx, JSValue this_val,
12131231
return JS_UNDEFINED;
12141232
}
12151233

1234+
JSValue QuickJSBinder::godot_set_script_metadata(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) {
1235+
ERR_FAIL_COND_V(argc < 2, JS_ThrowTypeError(ctx, "Two or more arguments expected"))
1236+
ERR_FAIL_COND_V(!JS_IsFunction(ctx, argv[0]), JS_ThrowTypeError(ctx, "godot class expected for argument #0"));
1237+
JSValue constructor = argv[0];
1238+
QuickJSBinder *binder = get_context_binder(ctx);
1239+
JS_SetProperty(ctx, constructor, binder->js_key_godot_tooled, JS_DupValue(ctx, argv[1]));
1240+
if (argc >= 3 && JS_IsString(argv[2])) {
1241+
JS_SetProperty(ctx, constructor, binder->js_key_godot_tooled, JS_DupValue(ctx, argv[2]));
1242+
}
1243+
return JS_UNDEFINED;
1244+
}
1245+
12161246
int QuickJSBinder::get_js_array_length(JSContext *ctx, JSValue p_val) {
12171247
if (!JS_IsArray(ctx, p_val)) return -1;
12181248
JSValue ret = JS_GetProperty(ctx, p_val, JS_ATOM_length);

quickjs/quickjs_binder.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ class QuickJSBinder : public ECMAScriptBinder {
5858
JSValue godot_object;
5959
JSValue empty_function;
6060
JSAtom js_key_godot_classid;
61+
JSAtom js_key_godot_tooled;
62+
JSAtom js_key_godot_icon_path;
6163
JSAtom js_key_godot_exports;
6264
JSAtom js_key_godot_signals;
6365
Vector<JSValue> godot_singletons;
@@ -119,6 +121,7 @@ class QuickJSBinder : public ECMAScriptBinder {
119121
static JSValue godot_register_class(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv);
120122
static JSValue godot_register_signal(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv);
121123
static JSValue godot_register_property(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv);
124+
static JSValue godot_set_script_metadata(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv);
122125

123126
_FORCE_INLINE_ static JSValue js_empty_func(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) { return JS_UNDEFINED; }
124127
_FORCE_INLINE_ static JSValue js_empty_consturctor(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) { return JS_NewObject(ctx); }

0 commit comments

Comments
 (0)