diff options
| author | bptato <nincsnevem662@gmail.com> | 2024-04-15 22:09:32 +0200 |
|---|---|---|
| committer | bptato <nincsnevem662@gmail.com> | 2024-04-15 22:13:51 +0200 |
| commit | 0d8ebdce2897244d8297eecd175441e541292b94 (patch) | |
| tree | 8331642e3a2163530651801c21556ef39c8ecdaa /src/js | |
| parent | 8df9ef9cbbd284afac4d26494d45253a43aa3146 (diff) | |
| download | chawan-0d8ebdce2897244d8297eecd175441e541292b94.tar.gz | |
js: remove automatic function -> closure conversion
It's a bad idea for several reasons:
* it's inefficient; must allocate an environment for a closure in Nim,
even though we already have one in JS
* writing macros for automatically creating functions with variadic
arguments is suprisingly difficult (see the entire `js/javascript'
module)
* it never really worked properly, because we never freed the associated
function pointer.
We hardly used it anyway, so the easiest fix is to get rid of it
completely.
Diffstat (limited to 'src/js')
| -rw-r--r-- | src/js/fromjs.nim | 58 | ||||
| -rw-r--r-- | src/js/javascript.nim | 26 | ||||
| -rw-r--r-- | src/js/jstypes.nim | 8 |
3 files changed, 24 insertions, 68 deletions
diff --git a/src/js/fromjs.nim b/src/js/fromjs.nim index a176cd33..f93e89fa 100644 --- a/src/js/fromjs.nim +++ b/src/js/fromjs.nim @@ -8,7 +8,6 @@ import io/promise import js/error import js/jstypes import js/opaque -import js/tojs import types/opt import utils/twtstr @@ -280,61 +279,6 @@ proc fromJSTable[A, B](ctx: JSContext, val: JSValue): JSResult[Table[A, B]] = res[kn] = vn return ok(res) -#TODO varargs -proc fromJSFunction1*[T, U](ctx: JSContext, val: JSValue): - proc(x: U): JSResult[T] = - #TODO this leaks memory! - 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, dupval, JS_UNDEFINED, 1, addr arg1) - result = fromJS[T](ctx, ret) - JS_FreeValue(ctx, ret) - -proc isErrType(rt: NimNode): bool = - let rtType = rt[0] - let errType = getTypeInst(Err) - return errType.sameType(rtType) and rtType.sameType(errType) - -# unpack brackets -proc getRealTypeFun(x: NimNode): NimNode = - var x = x.getTypeImpl() - while true: - if x.kind == nnkBracketExpr and x.len == 2: - x = x[1].getTypeImpl() - continue - break - return x - -macro unpackReturnType(f: typed) = - var x = f.getRealTypeFun() - let params = x.findChild(it.kind == nnkFormalParams) - let rv = params[0] - if rv.isErrType(): - return quote do: void - let rvv = rv[1] - return quote do: `rvv` - -macro unpackArg0(f: typed) = - var x = f.getRealTypeFun() - let params = x.findChild(it.kind == nnkFormalParams) - let rv = params[1] - doAssert rv.kind == nnkIdentDefs - let rvv = rv[1] - return quote do: `rvv` - -proc fromJSFunction[T](ctx: JSContext, val: JSValue): - JSResult[T] = - #TODO all args... - if not JS_IsFunction(ctx, val): - return err(newTypeError("function expected")) - return ok( - fromJSFunction1[ - typeof(unpackReturnType(T)), - typeof(unpackArg0(T)) - ](ctx, val)) - template optionType[T](o: type Option[T]): auto = T @@ -462,8 +406,6 @@ macro fromJS2(ctx: JSContext; val: JSValue; x: static string): untyped = proc fromJS*[T](ctx: JSContext, val: JSValue): JSResult[T] = when T is string: return fromJSString(ctx, val) - elif T is (proc): - return fromJSFunction[T](ctx, val) elif T is Option: return fromJSOption[optionType(T)](ctx, val) elif T is seq: diff --git a/src/js/javascript.nim b/src/js/javascript.nim index f2cd8a10..ef848038 100644 --- a/src/js/javascript.nim +++ b/src/js/javascript.nim @@ -185,7 +185,7 @@ proc free*(rt: var JSRuntime) = runtimes.del(runtimes.find(rt)) rt = nil -proc setGlobal*[T](ctx: JSContext, global: JSValue, obj: T) = +proc setGlobal*[T](ctx: JSContext; global: JSValue; obj: T) = # Add JSValue reference. let p = JS_VALUE_GET_PTR(global) let header = cast[ptr JSRefCountHeader](p) @@ -193,10 +193,11 @@ proc setGlobal*[T](ctx: JSContext, global: JSValue, obj: T) = ctx.setOpaque(global, cast[pointer](obj)) GC_ref(obj) -proc setInterruptHandler*(rt: JSRuntime, cb: JSInterruptHandler, opaque: pointer = nil) = +proc setInterruptHandler*(rt: JSRuntime; cb: JSInterruptHandler; + opaque: pointer = nil) = JS_SetInterruptHandler(rt, cb, opaque) -proc getExceptionStr*(ctx: JSContext): string = +proc getExceptionMsg*(ctx: JSContext): string = result = "" let ex = JS_GetException(ctx) let str = fromJS[string](ctx, ex) @@ -208,11 +209,20 @@ proc getExceptionStr*(ctx: JSContext): string = JS_FreeValue(ctx, stack) JS_FreeValue(ctx, ex) -proc writeException*(ctx: JSContext, s: DynStream) = - s.write(ctx.getExceptionStr()) +proc getExceptionMsg*(ctx: JSContext; err: JSError): string = + if err != nil: + JS_FreeValue(ctx, ctx.toJS(err)) # note: this implicitly throws + return ctx.getExceptionMsg() + +proc writeException*(ctx: JSContext; s: DynStream) = + s.write(ctx.getExceptionMsg()) + s.sflush() + +proc writeException*(ctx: JSContext; s: DynStream; err: JSError) = + s.write(ctx.getExceptionMsg(err)) s.sflush() -proc runJSJobs*(rt: JSRuntime, err: DynStream) = +proc runJSJobs*(rt: JSRuntime; err: DynStream) = while JS_IsJobPending(rt): var ctx: JSContext let r = JS_ExecutePendingJob(rt, addr ctx) @@ -322,10 +332,6 @@ func getMinArgs(params: seq[FuncParam]): int = func fromJSP[T: string|uint32](ctx: JSContext, atom: JSAtom): Opt[T] = return fromJS[T](ctx, atom) -proc getJSFunction*[T, U](ctx: JSContext, val: JSValue): - (proc(x: T): JSResult[U]) = - return fromJSFunction1[T, U](ctx, val) - proc defineConsts*[T](ctx: JSContext, classid: JSClassID, consts: static openArray[(string, T)]) = let proto = ctx.getOpaque().ctors[classid] diff --git a/src/js/jstypes.nim b/src/js/jstypes.nim index 5336f067..9e1d72ea 100644 --- a/src/js/jstypes.nim +++ b/src/js/jstypes.nim @@ -33,3 +33,11 @@ type func high*(abuf: JSArrayBuffer): int = return int(abuf.len) - 1 + +# A specialization of JSValue to make writing generic code for functions +# easier. +type JSValueFunction* = ref object + fun*: JSValue + +converter toJSValue*(f: JSValueFunction): JSValue = + f.fun |