[quickjs-devel] JSValue as pointer

  • From: "Serghei Amelian" <dmarc-noreply@xxxxxxxxxxxxx> (Redacted sender "serghei" for DMARC)
  • To: quickjs-devel@xxxxxxxxxxxxx
  • Date: Wed, 21 Aug 2019 17:22:19 +0300

Hello,

JSValue can carry reliable a pointer? At this moment it seems that the only 
method is to cast the pointer to an int64, but it looks a bit hacky to me.

Example:

===========================

Convert the pointer to int64:

[...]
JSValue ptr[1] = { JS_NewInt64(ctx, reinterpret_cast<int64_t>(this)) };
JS_SetPropertyStr(ctx, global_obj, "print", JS_NewCFunctionData(ctx, js_print, 
1, 0, 1, ptr));
[...]

Convert back to pointer:

static JSValue js_print(JSContext *ctx, JSValue this_val, int argc, JSValue 
*argv, int magic, JSValue *func_data)
{
    ICEJsEngine *jse;
    JS_ToIndex(ctx, (uint64_t*)&jse, *func_data);
    [...]
}

===========================

Or maybe my approach is not right?

Thanks.

-- 
Serghei

Other related posts: