@@ -247,7 +247,6 @@ JSValue QuickJSBinder::variant_to_var(JSContext *ctx, const Variant p_var) {
247
247
ERR_FAIL_NULL_V (data, JS_UNDEFINED);
248
248
ERR_FAIL_NULL_V (data->ecma_object , JS_UNDEFINED);
249
249
ERR_FAIL_COND_V (data->context != ctx, (JS_UNDEFINED));
250
- QuickJSBinder *binder = get_context_binder (ctx);
251
250
JSValue js_obj = JS_MKPTR (JS_TAG_OBJECT, data->ecma_object );
252
251
JS_DupValue (ctx, js_obj);
253
252
@@ -555,9 +554,7 @@ void QuickJSBinder::add_debug_binding_info(JSContext *ctx, JSValueConst p_obj, c
555
554
if (p_bind->type != Variant::OBJECT) {
556
555
classname = to_js_string (ctx, Variant::get_type_name (p_bind->type ));
557
556
} else {
558
- if (p_bind->is_reference ()) {
559
- classname = to_js_string (ctx, (*p_bind->godot_reference )->get_class_name ());
560
- } else if (p_bind->is_object ()) {
557
+ if (p_bind->is_object ()) {
561
558
classname = to_js_string (ctx, p_bind->godot_object ->get_class_name ());
562
559
}
563
560
}
@@ -1558,7 +1555,7 @@ ECMAScriptGCHandler *QuickJSBinder::alloc_object_binding_data(Object *p_object)
1558
1555
1559
1556
void QuickJSBinder::free_object_binding_data (ECMAScriptGCHandler *p_gc_handle) {
1560
1557
ECMAScriptGCHandler *bind = (ECMAScriptGCHandler *)p_gc_handle;
1561
- if (bind->is_object ()) {
1558
+ if (! bind->is_ref_counted ()) {
1562
1559
JSValue js_obj = JS_MKPTR (JS_TAG_OBJECT, bind->ecma_object );
1563
1560
JS_SetOpaque (js_obj, NULL );
1564
1561
JS_FreeValue ((JSContext *)bind->context , js_obj);
@@ -1585,21 +1582,9 @@ Error QuickJSBinder::bind_gc_object(JSContext *ctx, ECMAScriptGCHandler *data, O
1585
1582
data->type = Variant::OBJECT;
1586
1583
data->flags = ECMAScriptGCHandler::FLAG_OBJECT;
1587
1584
if (p_object->is_ref_counted ()) {
1588
- RefCounted *ref = Object::cast_to<RefCounted>(p_object);
1589
- data->flags |= ECMAScriptGCHandler::FLAG_REFERENCE;
1590
- #if 0
1591
- union {
1592
- Ref<RefCounted> *ref;
1593
- struct {
1594
- RefCounted *ref;
1595
- } * r;
1596
- } u;
1597
- u.ref = memnew(Ref<RefCounted>);
1598
- u.r->ref = ref;
1599
- data->godot_reference = u.ref;
1600
- #else
1601
- data->godot_reference = memnew (Ref<RefCounted>(ref));
1602
- #endif
1585
+ if (static_cast <RefCounted *>(p_object)->init_ref ()) {
1586
+ data->flags |= ECMAScriptGCHandler::FLAG_REF_COUNTED;
1587
+ }
1603
1588
}
1604
1589
JS_SetOpaque (obj, data);
1605
1590
#ifdef DUMP_LEAKS
@@ -1612,14 +1597,14 @@ Error QuickJSBinder::bind_gc_object(JSContext *ctx, ECMAScriptGCHandler *data, O
1612
1597
}
1613
1598
1614
1599
void QuickJSBinder::godot_refcount_incremented (ECMAScriptGCHandler *bind) {
1615
- if (bind->is_valid_ecma_object () && bind-> is_reference () ) {
1600
+ if (bind->is_valid_ecma_object ()) {
1616
1601
JSValue js_obj = JS_MKPTR (JS_TAG_OBJECT, bind->ecma_object );
1617
1602
JS_DupValue ((JSContext *)bind->context , js_obj);
1618
1603
}
1619
1604
}
1620
1605
1621
1606
bool QuickJSBinder::godot_refcount_decremented (ECMAScriptGCHandler *bind) {
1622
- if (bind->is_valid_ecma_object () && bind-> is_reference () ) {
1607
+ if (bind->is_valid_ecma_object ()) {
1623
1608
JSValue js_obj = JS_MKPTR (JS_TAG_OBJECT, bind->ecma_object );
1624
1609
JS_FreeValue ((JSContext *)bind->context , js_obj);
1625
1610
return bind->is_finalized ();
@@ -1669,9 +1654,7 @@ JSValue QuickJSBinder::object_constructor(JSContext *ctx, JSValueConst new_targe
1669
1654
JS_FreeValue(ctx, es_class_name);
1670
1655
gd_obj->set_script_instance(si);
1671
1656
#endif
1672
- if (bind->is_reference ()) {
1673
- bind->flags |= ECMAScriptGCHandler::FLAG_REFERENCE;
1674
- } else if (bind->is_object ()) {
1657
+ if (!bind->is_ref_counted ()) { // Object need to be freed manually
1675
1658
JS_DupValue (ctx, js_obj);
1676
1659
}
1677
1660
}
@@ -1696,17 +1679,19 @@ void QuickJSBinder::initialize_properties(JSContext *ctx, const ECMAClassInfo *p
1696
1679
1697
1680
void QuickJSBinder::object_finalizer (ECMAScriptGCHandler *p_bind) {
1698
1681
p_bind->flags ^= ECMAScriptGCHandler::FLAG_OBJECT;
1699
- if (p_bind->is_reference ()) {
1700
- p_bind->flags ^= ECMAScriptGCHandler::FLAG_REFERENCE;
1701
- memdelete (p_bind->godot_reference );
1682
+ if (p_bind->godot_object ->is_ref_counted ()) {
1683
+ RefCounted *ref = static_cast <RefCounted *>(p_bind->godot_object );
1684
+ if (ref->unreference ()) {
1685
+ memdelete (ref);
1686
+ }
1702
1687
}
1703
1688
}
1704
1689
1705
1690
void QuickJSBinder::origin_finalizer (JSRuntime *rt, JSValue val) {
1706
1691
QuickJSBinder *binder = get_runtime_binder (rt);
1707
1692
ECMAScriptGCHandler *bind = static_cast <ECMAScriptGCHandler *>(JS_GetOpaque (val, binder->godot_origin_class .class_id ));
1708
1693
if (bind) {
1709
- bind->flags |= ECMAScriptGCHandler::FLAG_SCRIPT_FINALIZED ;
1694
+ bind->flags |= ECMAScriptGCHandler::FLAG_FINALIZED ;
1710
1695
if (bind->type == Variant::OBJECT) {
1711
1696
object_finalizer (bind);
1712
1697
} else {
@@ -1720,7 +1705,7 @@ JSValue QuickJSBinder::object_free(JSContext *ctx, JSValue this_val, int argc, J
1720
1705
ECMAScriptGCHandler *bind = BINDING_DATA_FROM_JS (ctx, this_val);
1721
1706
ERR_FAIL_NULL_V (bind, JS_ThrowReferenceError (ctx, " The object already be freed" ));
1722
1707
ERR_FAIL_NULL_V (bind->godot_object , JS_ThrowReferenceError (ctx, " The object already be freed" ));
1723
- ERR_FAIL_COND_V (( bind->is_reference () ), JS_ThrowReferenceError (ctx, " Call free to RefCounted object is not allowed" ));
1708
+ ERR_FAIL_COND_V (bind->godot_object -> is_ref_counted ( ), JS_ThrowReferenceError (ctx, " Call free to RefCounted object is not allowed" ));
1724
1709
1725
1710
memdelete (bind->godot_object );
1726
1711
JS_SetOpaque (this_val, NULL );
@@ -2266,16 +2251,15 @@ JSValue QuickJSBinder::godot_abandon_value(JSContext *ctx, JSValue this_val, int
2266
2251
ECMAScriptGCHandler *data = BINDING_DATA_FROM_JS (ctx, value);
2267
2252
if (data) {
2268
2253
JS_SetOpaque (value, NULL );
2269
- if (data->type == Variant::OBJECT) {
2254
+ if (data->is_ref_counted ()) {
2255
+ static_cast <RefCounted *>(data->godot_object )->unreference ();
2256
+ } else if (data->is_object ()) {
2270
2257
JS_FreeValue (ctx, value);
2271
- if (data->is_reference ()) {
2272
- memdelete (data->godot_reference );
2273
- }
2274
2258
}
2275
2259
data->godot_object = NULL ;
2276
2260
data->ecma_object = NULL ;
2277
2261
data->context = NULL ;
2278
- data->flags |= ECMAScriptGCHandler::FLAG_CONTEXT_TRANSFERABLE ;
2262
+ data->flags |= ECMAScriptGCHandler::FLAG_TRANSFERABLE ;
2279
2263
} else {
2280
2264
valid = false ;
2281
2265
}
0 commit comments