Skip to content

Commit

Permalink
Construct Pool*Array from ArrayBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
Geequlim committed Jul 4, 2020
1 parent e6c735a commit 09ea5d1
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 11 deletions.
8 changes: 6 additions & 2 deletions misc/godot.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1889,6 +1889,7 @@ declare module godot {

constructor(source?: number[]);
constructor(from: PoolByteArray);
constructor(from: ArrayBuffer);

/** Appends an element at the end of the array (alias of `push_back`). */
append(byte: number) : void;
Expand Down Expand Up @@ -1954,7 +1955,7 @@ declare module godot {

constructor(source?: Color[]);
constructor(from: PoolColorArray);

constructor(from: ArrayBuffer);

/** Appends an element at the end of the array (alias of `push_back`). */
append(color: Color) : void;
Expand Down Expand Up @@ -2004,6 +2005,7 @@ declare module godot {

constructor(source?: number[]);
constructor(from: PoolIntArray);
constructor(from: ArrayBuffer);

/** Appends an element at the end of the array (alias of `push_back`). */
append(integer: number) : void;
Expand Down Expand Up @@ -2050,7 +2052,7 @@ declare module godot {

constructor(source?: number[]);
constructor(from: PoolRealArray);

constructor(from: ArrayBuffer);

/** Appends an element at the end of the array (alias of `push_back`). */
append(value: number) : void;
Expand Down Expand Up @@ -2144,6 +2146,7 @@ declare module godot {

constructor(source?: Vector2[]);
constructor(from: PoolVector2Array);
constructor(from: ArrayBuffer);

/** Appends an element at the end of the array (alias of `push_back`). */
append(vector2: Vector2) : void;
Expand Down Expand Up @@ -2190,6 +2193,7 @@ declare module godot {

constructor(source?: Vector3[]);
constructor(from: PoolVector3Array);
constructor(from: ArrayBuffer);

/** Appends an element at the end of the array (alias of `push_back`). */
append(vector3: Vector3) : void;
Expand Down
41 changes: 32 additions & 9 deletions quickjs/builtin_binding_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,38 @@ def generate_constructor(cls):
return QuickJSBuiltinBinder::bind_builtin_object_static(ctx, ${type}, ptr);
}
'''
TemplatePoolArrays = '''
TemplateSimplePoolArrays = '''
if (argc == 1) {
#ifdef DEBUG_METHODS_ENABLED
ERR_FAIL_COND_V(!JS_IsArray(ctx, argv[0]), (JS_ThrowTypeError(ctx, "Array expected for argument 0 of Pool*Array(from)")));
ERR_FAIL_COND_V(!JS_IsArray(ctx, argv[0]), (JS_ThrowTypeError(ctx, "Array expected for argument #0 of ${class}(from)")));
#endif
Variant arr = QuickJSBinder::var_to_variant(ctx, argv[0]);
ptr->operator=(arr);
}
'''
TemplatePoolArrays = '''
if (argc == 1) {
if (JS_IsArray(ctx, argv[0])) {
Variant arr = QuickJSBinder::var_to_variant(ctx, argv[0]);
ptr->operator=(arr);
} else if (JS_IsArrayBuffer(argv[0])) {
size_t size;
uint8_t *buffer = JS_GetArrayBuffer(ctx, &size, argv[0]);
if (size) {
if (size % sizeof(${element}) != 0) {
ERR_PRINTS("Length of the ArrayBuffer does not match for ${class}");
}
ptr->resize(size / sizeof(${element}));
copymem(ptr->write().ptr(), buffer, size / sizeof(${element}) * sizeof(${element}));
}
} else {
memdelete(ptr);
#ifdef DEBUG_METHODS_ENABLED
ERR_FAIL_COND_V(false, (JS_ThrowTypeError(ctx, "Array or ArrayBuffer expected for argument #0 of ${class}(from)")));
#endif
}
}
'''
ConstructorInitializers = {
"Vector2": '''
if (argc == 2) {
Expand Down Expand Up @@ -367,13 +390,13 @@ def generate_constructor(cls):
}
}
''',
"PoolByteArray": TemplatePoolArrays,
"PoolIntArray": TemplatePoolArrays,
"PoolRealArray": TemplatePoolArrays,
"PoolStringArray": TemplatePoolArrays,
"PoolVector2Array": TemplatePoolArrays,
"PoolVector3Array": TemplatePoolArrays,
"PoolColorArray": TemplatePoolArrays,
"PoolByteArray": apply_parttern(TemplatePoolArrays, {"class": "PoolByteArray", "type": "Variant::POOL_BYTE_ARRAY", "element": "uint8_t"}),
"PoolIntArray": apply_parttern(TemplatePoolArrays, {"class": "PoolIntArray", "type": "Variant::POOL_INT_ARRAY", "element": "int"}),
"PoolRealArray": apply_parttern(TemplatePoolArrays, {"class": "PoolRealArray", "type": "Variant::POOL_REAL_ARRAY", "element": "real_t"}),
"PoolVector2Array": apply_parttern(TemplatePoolArrays, {"class": "PoolVector2Array", "type": "Variant::POOL_VECTOR2_ARRAY", "element": "Vector2"}),
"PoolVector3Array": apply_parttern(TemplatePoolArrays, {"class": "PoolVector3Array", "type": "Variant::POOL_VECTOR3_ARRAY", "element": "Vector3"}),
"PoolColorArray": apply_parttern(TemplatePoolArrays, {"class": "PoolColorArray", "type": "Variant::POOL_COLOR_ARRAY", "element": "Color"}),
"PoolStringArray": apply_parttern(TemplateSimplePoolArrays,{"class": "PoolStringArray", "type": "Variant::POOL_STRING_ARRAY", "element": "String"}),
}
class_name = cls['name']
constructor_name = apply_parttern(TemplateConstructorName, {"class": class_name})
Expand Down
11 changes: 11 additions & 0 deletions quickjs/quickjs/quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -52565,3 +52565,14 @@ int JS_GetRefCount(JSValue v) {
}
return 0;
}

/* return -1 if exception (proxy case) or TRUE/FALSE */
JS_BOOL JS_IsArrayBuffer(JSValueConst val) {
JSObject *p;
if (JS_VALUE_GET_TAG(val) == JS_TAG_OBJECT) {
p = JS_VALUE_GET_OBJ(val);
return p->class_id == JS_CLASS_ARRAY_BUFFER;
} else {
return FALSE;
}
}
1 change: 1 addition & 0 deletions quickjs/quickjs/quickjs.h
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,7 @@ JSValue JS_GetStackFunction(JSContext *ctx, int back_level);
JS_BOOL JS_IsPureCFunction(JSContext *ctx, JSValue val);
const JSMallocState *JS_GetMollocState(JSRuntime *rt);
int JS_GetRefCount(JSValue val);
JS_BOOL JS_IsArrayBuffer(JSValueConst val);

#undef js_unlikely
#undef js_force_inline
Expand Down

0 comments on commit 09ea5d1

Please sign in to comment.