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/fromjs.nim | |
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/fromjs.nim')
-rw-r--r-- | src/js/fromjs.nim | 58 |
1 files changed, 0 insertions, 58 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: |