From 8b78543eeb81aa40e1562bd702ccf38c6538547f Mon Sep 17 00:00:00 2001 From: bptato Date: Wed, 30 Aug 2023 19:45:21 +0200 Subject: javascript: fix fromJSFunction, simplify setOpaque * fromJSFunction: dup the value, so that it cannot go out of context. * setOpaque no longer calls GC_ref, so no need for it to be generic instead of just taking a pointer. --- src/js/fromjs.nim | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/js/fromjs.nim') diff --git a/src/js/fromjs.nim b/src/js/fromjs.nim index 9c356da0..c7e27110 100644 --- a/src/js/fromjs.nim +++ b/src/js/fromjs.nim @@ -267,12 +267,14 @@ proc fromJSTable[A, B](ctx: JSContext, val: JSValue): JSResult[Table[A, B]] = #TODO varargs proc fromJSFunction1*[T, U](ctx: JSContext, val: JSValue): proc(x: U): JSResult[T] = + let dupval = JS_DupValue(ctx, JS_DupValue(ctx, val)) # save return proc(x: U): JSResult[T] = var arg1 = toJS(ctx, x) #TODO exceptions? - let ret = JS_Call(ctx, val, JS_UNDEFINED, 1, addr arg1) + let ret = JS_Call(ctx, dupval, JS_UNDEFINED, 1, addr arg1) when T isnot void: result = fromJS[T](ctx, ret) + JS_FreeValue(ctx, dupval) JS_FreeValue(ctx, ret) proc isErrType(rt: NimNode): bool = -- cgit 1.4.1-2-gfad0