@@ -409,44 +409,34 @@ extern "C"
409
409
410
410
411
411
412
-
413
- /* return NULL if not an object of class class_id */
414
- // JSClassID JS_GetClassID(JSValueConst obj)
415
- // {
416
- // JSObject *p;
417
- // if (JS_VALUE_GET_TAG(obj) != JS_TAG_OBJECT)
418
- // return NULL;
419
- // p = JS_VALUE_GET_OBJ(obj);
420
- // return p->class_id;
421
- // }
422
-
423
- // DLLEXPORT uint32_t jsNewClass(JSContext *ctx, const char *name)
424
- // {
425
- // JSClassID QJSClassId = 0;
426
- // JS_NewClassID(&QJSClassId);
427
- // JSRuntime *rt = JS_GetRuntime(ctx);
428
- // if (!JS_IsRegisteredClass(rt, QJSClassId))
429
- // {
430
- // JSClassDef def{
431
- // name,
432
- // // destructor
433
- // [](JSRuntime *rt, JSValue obj) noexcept {
434
- // JSClassID classid = JS_GetClassID(obj);
435
- // void *opaque = JS_GetOpaque(obj, classid);
436
- // JSChannel *channel = (JSChannel *)JS_GetRuntimeOpaque(rt);
437
- // if (channel == nullptr)
438
- // return;
439
- // channel((JSContext *)rt, JSChannelType_FREE_OBJECT, opaque);
440
- // }};
441
- // int e = JS_NewClass(rt, QJSClassId, &def);
442
- // if (e < 0)
443
- // {
444
- // JS_ThrowInternalError(ctx, "Cant register class %s", name);
445
- // return 0;
446
- // }
447
- // }
448
- // return QJSClassId;
449
- // }
412
+ DLLEXPORT uint32_t jsNewClass (JSContext *ctx, const char *name)
413
+ {
414
+ JSClassID QJSClassId = 0 ;
415
+ JS_NewClassID (&QJSClassId);
416
+ JSRuntime *rt = JS_GetRuntime (ctx);
417
+ if (!JS_IsRegisteredClass (rt, QJSClassId))
418
+ {
419
+ JSClassDef def{
420
+ name,
421
+ // destructor
422
+ [](JSRuntime *rt, JSValue obj) noexcept
423
+ {
424
+ JSClassID classid = JS_GetClassID (obj);
425
+ void *opaque = JS_GetOpaque (obj, classid);
426
+ RuntimeOpaque *runtimeOpaque = (RuntimeOpaque *)JS_GetRuntimeOpaque (rt);
427
+ if (runtimeOpaque == nullptr )
428
+ return ;
429
+ runtimeOpaque->channel ((JSContext *)rt, JSChannelType_FREE_OBJECT, opaque);
430
+ }};
431
+ int e = JS_NewClass (rt, QJSClassId, &def);
432
+ if (e < 0 )
433
+ {
434
+ JS_ThrowInternalError (ctx, " Cant register class %s" , name);
435
+ return 0 ;
436
+ }
437
+ }
438
+ return QJSClassId;
439
+ }
450
440
451
441
DLLEXPORT void *jsGetObjectOpaque (JSValue *obj, uint32_t classid)
452
442
{
0 commit comments