Skip to content

Commit

Permalink
node-nccc: Use external object instead of arraybuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
okuoku committed Feb 6, 2021
1 parent 263ef48 commit 3c15c21
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 33 deletions.
4 changes: 2 additions & 2 deletions jstestapp/ncccutil.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,11 +243,11 @@ function gencb(the_cb){
return freecb;
}

function wrapptr(ptr, size, freecb){ // => ptr
function wrapptr(ptr, freecb){ // => ptr
const cb = gencb(freecb);
const cba = node_nccc.make_nccc_cb(cb, "pp", "");
cb(cba[1], false);
return node_nccc.wrap_pointer(ptr, size, cba[0], cba[1], 999);
return node_nccc.wrap_pointer(ptr, cba[0], cba[1], 999);
}


Expand Down
2 changes: 1 addition & 1 deletion jstestapp/webgl-cwgl.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function readcstr(buf){
}

function wrapPointer(obj, relcb){
return ncccutil.wrapptr(ncccutil.ptraddr(obj), 1, relcb);
return ncccutil.wrapptr(ncccutil.ptraddr(obj), relcb);
}

function ptralloc(){
Expand Down
50 changes: 20 additions & 30 deletions node-nccc/node-nccc.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ static void
value_in(napi_env env, napi_value* vout, char type, uint64_t vin){
napi_status status;
status = napi_invalid_arg;
napi_value arraybuf;
switch(type){
case 'i':
status = napi_create_int32(env, vin, vout);
Expand All @@ -24,21 +23,14 @@ value_in(napi_env env, napi_value* vout, char type, uint64_t vin){
status = napi_create_int64(env, vin, vout);
break;
case 'p':
status = napi_create_external_arraybuffer(env,
(void*)(uintptr_t)vin,
1,
NULL,
NULL,
&arraybuf);
status = napi_create_external(env,
(void*)(uintptr_t)vin,
NULL,
NULL,
vout);
if(status != napi_ok){
abort();
}
status = napi_create_typedarray(env,
napi_uint8_array,
1,
arraybuf,
0,
vout);
break;
case 'f':
status = napi_create_double(env, *((float *)&vin), vout);
Expand Down Expand Up @@ -130,6 +122,11 @@ value_out(napi_env env, uint64_t* vout, char type, napi_value vin){
}else{
*vout = 0;
}
}else if(typ == napi_external){
status = napi_get_value_external(env, vin, (void**)vout);
if(status != napi_ok){
abort();
}
}else if(typ == napi_string && type == 'p'){
strbuflen = 0;
status = napi_get_value_string_utf8(env,
Expand Down Expand Up @@ -246,7 +243,7 @@ do_finalize(napi_env env, void* data, void* ctx){

static napi_value
nccc_wrap_pointer(napi_env env, napi_callback_info info){
// [ptr size dispatch ctx arg] => ptr
// [ptr dispatch ctx arg] => ptr
// [dispatch,ctx] = [arg ptr] => []
napi_status status;
size_t argc;
Expand All @@ -261,7 +258,7 @@ nccc_wrap_pointer(napi_env env, napi_callback_info info){

params = malloc(sizeof(finalizer_params_t));

argc = 5;
argc = 4;
status = napi_get_cb_info(env, info, &argc, args, &ctx_this, &bogus);
if(status != napi_ok){
abort();
Expand All @@ -272,38 +269,31 @@ nccc_wrap_pointer(napi_env env, napi_callback_info info){
abort();
}
ptr = (void*)(intptr_t)out;
// size
status = napi_get_value_int64(env, args[1], &out);
if(status != napi_ok){
abort();
}
size = out;
// dispatch
status = napi_get_value_int64(env, args[2], &out);
status = napi_get_value_int64(env, args[1], &out);
if(status != napi_ok){
abort();
}
params->dispatch = out;
// ctx
status = napi_get_value_int64(env, args[3], &out);
status = napi_get_value_int64(env, args[2], &out);
if(status != napi_ok){
abort();
}
params->ctx = out;
// arg
status = napi_get_value_int64(env, args[4], &out);
status = napi_get_value_int64(env, args[3], &out);
if(status != napi_ok){
abort();
}
params->arg = out;

/* Regenerate ArrayBuffer */
status = napi_create_external_arraybuffer(env,
ptr,
size,
do_finalize,
params,
&ret);
status = napi_create_external(env,
ptr,
do_finalize,
params,
&ret);
if(status != napi_ok){
abort();
}
Expand Down

0 comments on commit 3c15c21

Please sign in to comment.