From 01c3798854ecf9ec70af51ad0569a540f8902993 Mon Sep 17 00:00:00 2001 From: jiepengtan Date: Mon, 16 Sep 2024 11:05:14 +0800 Subject: [PATCH] Add more js call cpp util functions --- platform/web/godot_js_spx_util.cpp | 217 +++++++++++++----------- platform/web/js/engine/gdspx.util.js | 240 ++++++++++++++++++++------- 2 files changed, 300 insertions(+), 157 deletions(-) diff --git a/platform/web/godot_js_spx_util.cpp b/platform/web/godot_js_spx_util.cpp index 15cf41d0fc3a..7b01d80399a1 100644 --- a/platform/web/godot_js_spx_util.cpp +++ b/platform/web/godot_js_spx_util.cpp @@ -1,4 +1,3 @@ - #include "core/extension/gdextension_spx_ext.h" #include "core/extension/gdextension.h" @@ -20,59 +19,66 @@ static ObjectPool rect2Pool(100); extern "C" { - +// other functions EMSCRIPTEN_KEEPALIVE float gdspx_get_value(float* array, int idx) { return array[idx]; } +// bool functions EMSCRIPTEN_KEEPALIVE GdBool* gdspx_alloc_bool() { return boolPool.acquire(); } EMSCRIPTEN_KEEPALIVE -GDFloat* gdspx_alloc_float() { - return floatPool.acquire(); +GdBool* gdspx_new_bool(bool val) { + GdBool* ptr = gdspx_alloc_bool(); + *ptr = (GdBool)val; + return ptr; } EMSCRIPTEN_KEEPALIVE -GdInt* gdspx_alloc_int() { - return intPool.acquire(); +void gdspx_free_bool(GdBool* b) { + boolPool.release(b); } + EMSCRIPTEN_KEEPALIVE -GdObj* gdspx_alloc_obj() { - return objPool.acquire(); +void gdspx_print_bool(GdBool* value) { + print_line("bool: %s", *value ? "true" : "false"); } +// float functions EMSCRIPTEN_KEEPALIVE -GdVec2* gdspx_alloc_vec2() { - return vec2Pool.acquire(); +GdFloat* gdspx_alloc_float() { + return floatPool.acquire(); } EMSCRIPTEN_KEEPALIVE -GdVec3* gdspx_alloc_vec3() { - return vec3Pool.acquire(); +GdFloat* gdspx_new_float(float val) { + GdFloat* ptr = gdspx_alloc_float(); + *ptr = (GdFloat)val; + return ptr; } EMSCRIPTEN_KEEPALIVE -GdVec4* gdspx_alloc_vec4() { - return vec4Pool.acquire(); +void gdspx_free_float(GdFloat* f) { + floatPool.release(f); } EMSCRIPTEN_KEEPALIVE -GdColor* gdspx_alloc_color() { - return colorPool.acquire(); +void gdspx_print_float(GdFloat* value) { + print_line("float: %f", *value); } +// int functions EMSCRIPTEN_KEEPALIVE -GdRect2* gdspx_alloc_rect2() { - return rect2Pool.acquire(); +GdInt* gdspx_alloc_int() { + return intPool.acquire(); } -// EMSCRIPTEN_KEEPALIVE GdInt* gdspx_new_int(double val) { GdInt* ptr = gdspx_alloc_int(); @@ -81,59 +87,51 @@ GdInt* gdspx_new_int(double val) { } EMSCRIPTEN_KEEPALIVE -GdObj* gdspx_new_obj(double val) { - GdObj* ptr = gdspx_alloc_obj(); - *ptr = (GdObj)val; - return ptr; +void gdspx_free_int(GdInt* i) { + intPool.release(i); } EMSCRIPTEN_KEEPALIVE -GdVec2* gdspx_new_vec2(float x, float y) { - GdVec2* ptr = gdspx_alloc_vec2(); - ptr->x = x; - ptr->y = y; - return ptr; +void gdspx_print_int(GdInt* value) { + print_line("int: %d", *value); } +// object functions EMSCRIPTEN_KEEPALIVE -GdVec3* gdspx_new_vec3(float x, float y, float z) { - GdVec3* ptr= gdspx_alloc_vec3(); - ptr->x = x; - ptr->y = y; - ptr->z = z; - return ptr; +GdObj* gdspx_alloc_obj() { + return objPool.acquire(); } EMSCRIPTEN_KEEPALIVE -GdVec4* gdspx_new_vec4(float x, float y, float z, float w) { - GdVec4* ptr = gdspx_alloc_vec4(); - ptr->x = x; - ptr->y = y; - ptr->z = z; - ptr->w = w; +GdObj* gdspx_new_obj(double val) { + GdObj* ptr = gdspx_alloc_obj(); + *ptr = (GdObj)val; return ptr; } EMSCRIPTEN_KEEPALIVE -GdColor* gdspx_new_color(float r, float g, float b, float a) { - GdColor* ptr = gdspx_alloc_color(); - ptr->r = r; - ptr->g = g; - ptr->b = b; - ptr->a = a; - return ptr; +void gdspx_free_obj(GdObj* obj) { + objPool.release(obj); } EMSCRIPTEN_KEEPALIVE -GdRect2* gdspx_new_rect2(float x, float y, float width, float height) { - GdRect2* ptr = gdspx_alloc_rect2(); - ptr->position.x = x; - ptr->position.y = y; - ptr->size.width = width; - ptr->size.height = height; - return ptr; +void gdspx_print_obj(GdObj* obj) { + print_line("object: %p", (void*)obj); +} + +// vec2 functions +EMSCRIPTEN_KEEPALIVE +GdVec2* gdspx_alloc_vec2() { + return vec2Pool.acquire(); } +EMSCRIPTEN_KEEPALIVE +GdVec2* gdspx_new_vec2(float x, float y) { + GdVec2* ptr = gdspx_alloc_vec2(); + ptr->x = x; + ptr->y = y; + return ptr; +} EMSCRIPTEN_KEEPALIVE void gdspx_free_vec2(GdVec2* vec) { @@ -141,100 +139,128 @@ void gdspx_free_vec2(GdVec2* vec) { } EMSCRIPTEN_KEEPALIVE -void gdspx_free_string(GdString* str) { - stringPool.release(str); +void gdspx_print_vec2(GdVec2* vec) { + print_line("vec2: %f, %f", vec->x, vec->y); } - +// vec3 functions EMSCRIPTEN_KEEPALIVE -void gdspx_free_obj(GdObj* obj) { - objPool.release(obj); +GdVec3* gdspx_alloc_vec3() { + return vec3Pool.acquire(); } EMSCRIPTEN_KEEPALIVE -void gdspx_free_int(GdInt* i) { - intPool.release(i); +GdVec3* gdspx_new_vec3(float x, float y, float z) { + GdVec3* ptr= gdspx_alloc_vec3(); + ptr->x = x; + ptr->y = y; + ptr->z = z; + return ptr; } - EMSCRIPTEN_KEEPALIVE void gdspx_free_vec3(GdVec3* vec) { vec3Pool.release(vec); } - EMSCRIPTEN_KEEPALIVE -void gdspx_free_vec4(GdVec4* vec) { - vec4Pool.release(vec); +void gdspx_print_vec3(GdVec3* vec) { + print_line("vec3: %f, %f, %f", vec->x, vec->y, vec->z); } - +// vec4 functions EMSCRIPTEN_KEEPALIVE -void gdspx_free_color(GdColor* color) { - colorPool.release(color); +GdVec4* gdspx_alloc_vec4() { + return vec4Pool.acquire(); } - EMSCRIPTEN_KEEPALIVE -void gdspx_free_rect2(GdRect2* rect) { - rect2Pool.release(rect); +GdVec4* gdspx_new_vec4(float x, float y, float z, float w) { + GdVec4* ptr = gdspx_alloc_vec4(); + ptr->x = x; + ptr->y = y; + ptr->z = z; + ptr->w = w; + return ptr; } EMSCRIPTEN_KEEPALIVE -void gdspx_free_float(GdFloat* f) { - floatPool.release(f); +void gdspx_free_vec4(GdVec4* vec) { + vec4Pool.release(vec); } EMSCRIPTEN_KEEPALIVE -void gdspx_free_bool(GdBool* b) { - boolPool.release(b); +void gdspx_print_vec4(GdVec4* vec) { + print_line("vec4: %f, %f, %f, %f", vec->x, vec->y, vec->z, vec->w); } - - +// color functions EMSCRIPTEN_KEEPALIVE -void gdspx_print_vec2(GdVec2* vec) { - print_line("vec2: %f, %f", vec->x, vec->y); +GdColor* gdspx_alloc_color() { + return colorPool.acquire(); } - EMSCRIPTEN_KEEPALIVE -void gdspx_print_vec3(GdVec3* vec) { - print_line("vec3: %f, %f, %f", vec->x, vec->y, vec->z); +GdColor* gdspx_new_color(float r, float g, float b, float a) { + GdColor* ptr = gdspx_alloc_color(); + ptr->r = r; + ptr->g = g; + ptr->b = b; + ptr->a = a; + return ptr; } - EMSCRIPTEN_KEEPALIVE -void gdspx_print_vec4(GdVec4* vec) { - print_line("vec4: %f, %f, %f, %f", vec->x, vec->y, vec->z, vec->w); +void gdspx_free_color(GdColor* color) { + colorPool.release(color); } - EMSCRIPTEN_KEEPALIVE void gdspx_print_color(GdColor* color) { print_line("color: %f, %f, %f, %f", color->r, color->g, color->b, color->a); } +// rect2 functions +EMSCRIPTEN_KEEPALIVE +GdRect2* gdspx_alloc_rect2() { + return rect2Pool.acquire(); +} +EMSCRIPTEN_KEEPALIVE +GdRect2* gdspx_new_rect2(float x, float y, float width, float height) { + GdRect2* ptr = gdspx_alloc_rect2(); + ptr->position.x = x; + ptr->position.y = y; + ptr->size.width = width; + ptr->size.height = height; + return ptr; +} + +EMSCRIPTEN_KEEPALIVE +void gdspx_free_rect2(GdRect2* rect) { + rect2Pool.release(rect); +} EMSCRIPTEN_KEEPALIVE void gdspx_print_rect2(GdRect2* rect) { print_line("rect2: position(%f, %f), size(%f, %f)", rect->position.x, rect->position.y, rect->size.width, rect->size.height); } +// string functions EMSCRIPTEN_KEEPALIVE -void gdspx_print_int(GdInt* value) { - print_line("int: %d", *value); +GdString* gdspx_alloc_string() { + return stringPool.acquire(); } EMSCRIPTEN_KEEPALIVE -void gdspx_print_float(GdFloat* value) { - print_line("float: %f", *value); +GdString* gdspx_new_string(const char* str) { + GdString* ptr = gdspx_alloc_string(); + ptr->c_str = strdup(str); + return ptr; } -EMSCRIPTEN_KEEPALIVE -void gdspx_print_bool(GdBool* value) { - print_line("bool: %s", *value ? "true" : "false"); +void gdspx_free_string(GdString* str) { + stringPool.release(str); } EMSCRIPTEN_KEEPALIVE @@ -242,9 +268,4 @@ void gdspx_print_string(GdString* str) { print_line("string: %s", str->c_str()); } -EMSCRIPTEN_KEEPALIVE -void gdspx_print_obj(GdObj* obj) { - print_line("object: %p", (void*)obj); } - -} \ No newline at end of file diff --git a/platform/web/js/engine/gdspx.util.js b/platform/web/js/engine/gdspx.util.js index 4041770849bf..18af743f21d8 100644 --- a/platform/web/js/engine/gdspx.util.js +++ b/platform/web/js/engine/gdspx.util.js @@ -1,117 +1,239 @@ +// TODO @jiepengtan cache function pointer -function ToGdString(str) { - const encoder = new TextEncoder(); - const stringBytes = encoder.encode(str); - const ptr = Module._malloc(stringBytes.length + 1); - Module.HEAPU8.set(stringBytes, ptr); - Module.HEAPU8[ptr + stringBytes.length] = 0; - return ptr -} -function ToGdFloat(value) { - return value -} +// Bool-related functions function ToGdBool(value) { - return value + func = GodotEngine.rtenv['_gdspx_new_bool']; + return func(value); } -function ToGdObj(object) { - func = GodotEngine.rtenv['_gdspx_new_obj']; - return func(object) +function ToJsBool(ptr) { + const HEAPU8 = Module.HEAPU8; + const boolValue = HEAPU8[ptr]; + return boolValue !== 0; } -function ToGdVec2(vec) { - func = GodotEngine.rtenv['_gdspx_new_vec2']; - return func(vec.x, vec.y) +function AllocGdBool() { + return GodotEngine.rtenv['_gdspx_alloc_bool'](); } -function ToGdVec3(vec) { - func = GodotEngine.rtenv['_gdspx_new_vec3']; - return func(vec.x, vec.y, vec.z) + +function PrintGdBool(ptr) { + console.log(ToJsBool(ptr)); } -function ToGdVec4(vec) { - func = GodotEngine.rtenv['_gdspx_new_vec4']; - return func(vec.x, vec.y, vec.z, vec.w) +// Int-related functions +function ToJsInt(ptr) { + const memoryBuffer = Module.HEAPU8.buffer; + const dataView = new DataView(memoryBuffer); + const low = dataView.getUint32(ptr, true); // 低32位 + const high = dataView.getUint32(ptr + 4, true); // 高32位 + const int64Value = BigInt(high) << 32n | BigInt(low); + return int64Value; } -function ToGdColor(color) { - func = GodotEngine.rtenv['_gdspx_new_color']; - return func(color.r, color.g, color.b, color.a) +function AllocGdInt() { + ptr = GodotEngine.rtenv['_gdspx_alloc_int']; + return ptr; } -function ToGdRect2(rect) { - func = GodotEngine.rtenv['_gdspx_new_rect2']; - return func(rect.position.x, rect.position.y, rect.size.width, rect.size.height) +function PrintGdInt(ptr) { + console.log(ToJsInt(ptr)); } -function AllocGdInt() { - ptr = GodotEngine.rtenv['_gdspx_alloc_int']; - return ptr +// Float-related functions +function ToGdFloat(value) { + func = GodotEngine.rtenv['_gdspx_new_float']; + return func(value); } -function AllocGdBool() { - return GodotEngine.rtenv['_gdspx_alloc_bool'](); +function ToJsFloat(ptr) { + const HEAPF32 = Module.HEAPF32; + const floatIndex = ptr / 4; + const floatValue = HEAPF32[floatIndex]; + return floatValue; } function AllocGdFloat() { return GodotEngine.rtenv['_gdspx_alloc_float'](); } +function PrintGdFloat(ptr) { + console.log(ToJsFloat(ptr)); +} + +// String-related functions +function ToGdString(str) { + const encoder = new TextEncoder(); + const stringBytes = encoder.encode(str); + const ptr = Module._malloc(stringBytes.length + 1); + Module.HEAPU8.set(stringBytes, ptr); + Module.HEAPU8[ptr + stringBytes.length] = 0; + return ptr; +} + +function ToJsString(ptr) { + const HEAPU8 = Module.HEAPU8; + let length = 0; + while (HEAPU8[ptr + length] !== 0) { + length++; + } + const stringBytes = HEAPU8.subarray(ptr, ptr + length); + const decoder = new TextDecoder(); + return decoder.decode(stringBytes); +} + function AllocGdString() { return GodotEngine.rtenv['_gdspx_alloc_string'](); } +function PrintGdString(ptr) { + console.log(ToJsString(ptr)); +} + +// Object-related functions +function ToGdObj(object) { + func = GodotEngine.rtenv['_gdspx_new_obj']; + return func(object); +} + +function ToJsObj(ptr) { + const memoryBuffer = Module.HEAPU8.buffer; + const dataView = new DataView(memoryBuffer); + const low = dataView.getUint32(ptr, true); // 低32位 + const high = dataView.getUint32(ptr + 4, true); // 高32位 + const int64Value = BigInt(high) << 32n | BigInt(low); + return int64Value; +} + +function PrintGdObj(ptr) { + console.log(ToJsObj(ptr)); +} + +// Vec2-related functions +function ToGdVec2(vec) { + func = GodotEngine.rtenv['_gdspx_new_vec2']; + return func(vec.x, vec.y); +} + +function ToJsVec2(ptr) { + const HEAPF32 = Module.HEAPF32; + const floatIndex = ptr / 4; + return { + x: HEAPF32[floatIndex], + y: HEAPF32[floatIndex + 1] + }; +} + function AllocGdVec2() { return GodotEngine.rtenv['_gdspx_alloc_vec2'](); } +function PrintGdVec2(ptr) { + console.log(ToJsVec2(ptr)); +} + +// Vec3-related functions +function ToGdVec3(vec) { + func = GodotEngine.rtenv['_gdspx_new_vec3']; + return func(vec.x, vec.y, vec.z); +} + +function ToJsVec3(ptr) { + const HEAPF32 = Module.HEAPF32; + const floatIndex = ptr / 4; + return { + x: HEAPF32[floatIndex], + y: HEAPF32[floatIndex + 1], + z: HEAPF32[floatIndex + 2] + }; +} + function AllocGdVec3() { return GodotEngine.rtenv['_gdspx_alloc_vec3'](); } -function AllocGdVec4() { - return GodotEngine.rtenv['_gdspx_alloc_vec4'](); +function PrintGdVec3(ptr) { + const vec3 = ToJsVec3(ptr); + console.log(`Vec3(${vec3.x}, ${vec3.y}, ${vec3.z})`); } -function AllocGdColor() { - return GodotEngine.rtenv['_gdspx_alloc_color'](); +// Vec4-related functions +function ToGdVec4(vec) { + func = GodotEngine.rtenv['_gdspx_new_vec4']; + return func(vec.x, vec.y, vec.z, vec.w); } -function AllocGdRect2() { - return GodotEngine.rtenv['_gdspx_alloc_rect2'](); +function ToJsVec4(ptr) { + const HEAPF32 = Module.HEAPF32; + const floatIndex = ptr / 4; + return { + x: HEAPF32[floatIndex], + y: HEAPF32[floatIndex + 1], + z: HEAPF32[floatIndex + 2], + w: HEAPF32[floatIndex + 3] + }; } -function PrintGdFloat(obj) { - console.log(obj); +function AllocGdVec4() { + return GodotEngine.rtenv['_gdspx_alloc_vec4'](); } -function PrintGdBool(obj) { - console.log(obj); +function PrintGdVec4(ptr) { + const vec4 = ToJsVec4(ptr); + console.log(`Vec4(${vec4.x}, ${vec4.y}, ${vec4.z}, ${vec4.w})`); } -function PrintGdString(obj) { - GodotEngine.rtenv['_gdspx_print_string'](obj); +// Color-related functions +function ToGdColor(color) { + func = GodotEngine.rtenv['_gdspx_new_color']; + return func(color.r, color.g, color.b, color.a); } -function PrintGdVec4(obj) { - GodotEngine.rtenv['_gdspx_print_vec4'](obj); +function ToJsColor(ptr) { + const HEAPF32 = Module.HEAPF32; + const floatIndex = ptr / 4; + return { + r: HEAPF32[floatIndex], + g: HEAPF32[floatIndex + 1], + b: HEAPF32[floatIndex + 2], + a: HEAPF32[floatIndex + 3] + }; } -function PrintGdVec2(obj) { - GodotEngine.rtenv['_gdspx_print_vec2'](obj); + +function AllocGdColor() { + return GodotEngine.rtenv['_gdspx_alloc_color'](); } -function PrintGdVec3(obj) { - GodotEngine.rtenv['_gdspx_print_vec3'](obj); +function PrintGdColor(ptr) { + const color = ToJsColor(ptr); + console.log(`Color(${color.r}, ${color.g}, ${color.b}, ${color.a})`); } -function PrintGdColor(obj) { - GodotEngine.rtenv['_gdspx_print_color'](obj); +// Rect2-related functions +function ToGdRect2(rect) { + func = GodotEngine.rtenv['_gdspx_new_rect2']; + return func(rect.position.x, rect.position.y, rect.size.width, rect.size.height); } -function PrintGdRect2(obj) { - GodotEngine.rtenv['_gdspx_print_rect2'](obj); +function ToJsRect2(ptr) { + const HEAPF32 = Module.HEAPF32; + const floatIndex = ptr / 4; + return { + position: { + x: HEAPF32[floatIndex], + y: HEAPF32[floatIndex + 1] + }, + size: { + width: HEAPF32[floatIndex + 2], + height: HEAPF32[floatIndex + 3] + } + }; } -function PrintGdInt(obj) { - GodotEngine.rtenv['_gdspx_print_int'](obj); +function AllocGdRect2() { + return GodotEngine.rtenv['_gdspx_alloc_rect2'](); } +function PrintGdRect2(ptr) { + const rect = ToJsRect2(ptr); + console.log(`Rect2(position: (${rect.position.x}, ${rect.position.y}), size: (${rect.size.width}, ${rect.size.height}))`); +}