diff options
author | bptato <nincsnevem662@gmail.com> | 2024-04-16 15:08:44 +0200 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2024-04-17 23:19:09 +0200 |
commit | 66b9574b165be62e76c7397cf0eaa8d229d42675 (patch) | |
tree | adb8a9719cc70f2b577706aaa4e30bb0d5d629a6 /src/js | |
parent | d86f1939204eee771a30f47e4cbe71fd8d9a4f5f (diff) | |
download | chawan-66b9574b165be62e76c7397cf0eaa8d229d42675.tar.gz |
Update code style
* separate params with ; (semicolon) instead of , (colon) * reduce screaming snake case use * wrap long lines
Diffstat (limited to 'src/js')
-rw-r--r-- | src/js/base64.nim | 2 | ||||
-rw-r--r-- | src/js/console.nim | 14 | ||||
-rw-r--r-- | src/js/encoding.nim | 8 | ||||
-rw-r--r-- | src/js/fromjs.nim | 65 | ||||
-rw-r--r-- | src/js/intl.nim | 6 | ||||
-rw-r--r-- | src/js/javascript.nim | 246 | ||||
-rw-r--r-- | src/js/module.nim | 6 | ||||
-rw-r--r-- | src/js/opaque.nim | 18 | ||||
-rw-r--r-- | src/js/propertyenumlist.nim | 6 | ||||
-rw-r--r-- | src/js/regex.nim | 2 | ||||
-rw-r--r-- | src/js/tojs.nim | 156 |
11 files changed, 273 insertions, 256 deletions
diff --git a/src/js/base64.nim b/src/js/base64.nim index 3d540664..a8362910 100644 --- a/src/js/base64.nim +++ b/src/js/base64.nim @@ -17,7 +17,7 @@ proc atob*(data: string): DOMResult[NarrowString] = return errDOMException("Invalid character in string", "InvalidCharacterError") -proc btoa*(ctx: JSContext, data: JSValue): DOMResult[string] = +proc btoa*(ctx: JSContext; data: JSValue): DOMResult[string] = let data = JS_ToString(ctx, data) if JS_IsException(data): return err() diff --git a/src/js/console.nim b/src/js/console.nim index d5e074e8..305dfa9b 100644 --- a/src/js/console.nim +++ b/src/js/console.nim @@ -18,7 +18,7 @@ proc newConsole*(err: DynStream; clearFun: proc() = nil; showFun: proc() = nil; hideFun: hideFun ) -proc log*(console: Console, ss: varargs[string]) {.jsfunc.} = +proc log*(console: Console; ss: varargs[string]) {.jsfunc.} = for i in 0..<ss.len: console.err.write(ss[i]) if i != ss.high: @@ -31,23 +31,23 @@ proc clear(console: Console) {.jsfunc.} = console.clearFun() # For now, these are the same as log(). -proc debug(console: Console, ss: varargs[string]) {.jsfunc.} = +proc debug(console: Console; ss: varargs[string]) {.jsfunc.} = console.log(ss) -proc error(console: Console, ss: varargs[string]) {.jsfunc.} = +proc error(console: Console; ss: varargs[string]) {.jsfunc.} = console.log(ss) -proc info(console: Console, ss: varargs[string]) {.jsfunc.} = +proc info(console: Console; ss: varargs[string]) {.jsfunc.} = console.log(ss) -proc warn(console: Console, ss: varargs[string]) {.jsfunc.} = +proc warn(console: Console; ss: varargs[string]) {.jsfunc.} = console.log(ss) -proc show(console: Console, ss: varargs[string]) {.jsfunc.} = +proc show(console: Console; ss: varargs[string]) {.jsfunc.} = if console.showFun != nil: console.showFun() -proc hide(console: Console, ss: varargs[string]) {.jsfunc.} = +proc hide(console: Console; ss: varargs[string]) {.jsfunc.} = if console.hideFun != nil: console.hideFun() diff --git a/src/js/encoding.nim b/src/js/encoding.nim index eff99805..df4043b5 100644 --- a/src/js/encoding.nim +++ b/src/js/encoding.nim @@ -63,21 +63,21 @@ proc grow(buf: var Growbuf) = buf.cap *= 2 buf.p = cast[ptr UncheckedArray[uint8]](buf.p.realloc(buf.cap)) -proc write(buf: var Growbuf, s: openArray[uint8]) = +proc write(buf: var Growbuf; s: openArray[uint8]) = if buf.len + s.len > buf.cap: buf.grow() if s.len > 0: copyMem(addr buf.p[buf.len], unsafeAddr s[0], s.len) buf.len += s.len -proc write(buf: var Growbuf, s: string) = +proc write(buf: var Growbuf; s: string) = if buf.len + s.len > buf.cap: buf.grow() if s.len > 0: copyMem(addr buf.p[buf.len], unsafeAddr s[0], s.len) buf.len += s.len -proc decode0(this: JSTextDecoder, ctx: JSContext, input: JSArrayBufferView, +proc decode0(this: JSTextDecoder; ctx: JSContext; input: JSArrayBufferView; stream: bool): JSResult[JSValue] = var oq = Growbuf( p: cast[ptr UncheckedArray[uint8]](alloc(BufferSize)), @@ -106,7 +106,7 @@ proc decode0(this: JSTextDecoder, ctx: JSContext, input: JSArrayBufferView, oq.grow() return ok(JS_NewStringLen(ctx, cast[cstring](oq.p), csize_t(oq.len))) -proc validate0(this: JSTextDecoder, ctx: JSContext, input: JSArrayBufferView, +proc validate0(this: JSTextDecoder; ctx: JSContext; input: JSArrayBufferView; stream: bool): JSResult[JSValue] = # assume input is valid; do not allocate yet var oq = Growbuf(p: nil, len: 0, cap: 0) diff --git a/src/js/fromjs.nim b/src/js/fromjs.nim index f93e89fa..4cdc18d1 100644 --- a/src/js/fromjs.nim +++ b/src/js/fromjs.nim @@ -11,9 +11,9 @@ import js/opaque import types/opt import utils/twtstr -proc fromJS*[T](ctx: JSContext, val: JSValue): JSResult[T] +proc fromJS*[T](ctx: JSContext; val: JSValue): JSResult[T] -func isInstanceOfNonGlobal(ctx: JSContext, val: JSValue, class: string): bool = +func isInstanceOfNonGlobal(ctx: JSContext; val: JSValue; class: string): bool = let ctxOpaque = ctx.getOpaque() var classid = JS_GetClassID(val) let tclassid = ctxOpaque.creg[class] @@ -30,7 +30,7 @@ func isInstanceOfNonGlobal(ctx: JSContext, val: JSValue, class: string): bool = break return found -func isInstanceOfGlobal(ctx: JSContext, val: JSValue, class: string): bool = +func isInstanceOfGlobal(ctx: JSContext; val: JSValue; class: string): bool = let ctxOpaque = ctx.getOpaque() #TODO gparent only works for a single level. (But this is not really a # problem right now, because our global objects have at most one inheritance @@ -48,11 +48,11 @@ func isInstanceOfGlobal(ctx: JSContext, val: JSValue, class: string): bool = return true return false -func isInstanceOf*(ctx: JSContext, val: JSValue, class: string): bool = +func isInstanceOf*(ctx: JSContext; val: JSValue; class: string): bool = return ctx.isInstanceOfGlobal(val, class) or ctx.isInstanceOfNonGlobal(val, class) -func toString(ctx: JSContext, val: JSValue): Opt[string] = +func toString(ctx: JSContext; val: JSValue): Opt[string] = var plen: csize_t let outp = JS_ToCStringLen(ctx, addr plen, val) # cstring if outp != nil: @@ -63,7 +63,7 @@ func toString(ctx: JSContext, val: JSValue): Opt[string] = result = ok(ret) JS_FreeCString(ctx, outp) -func fromJSString(ctx: JSContext, val: JSValue): JSResult[string] = +func fromJSString(ctx: JSContext; val: JSValue): JSResult[string] = var plen: csize_t let outp = JS_ToCStringLen(ctx, addr plen, val) # cstring if outp == nil: @@ -75,7 +75,7 @@ func fromJSString(ctx: JSContext, val: JSValue): JSResult[string] = JS_FreeCString(ctx, outp) return ok(ret) -func fromJSInt[T: SomeInteger](ctx: JSContext, val: JSValue): +func fromJSInt[T: SomeInteger](ctx: JSContext; val: JSValue): JSResult[T] = when T is int: # Always int32, so we don't risk 32-bit only breakage. @@ -110,7 +110,7 @@ func fromJSInt[T: SomeInteger](ctx: JSContext, val: JSValue): return err() return ok(cast[uint64](ret)) -proc fromJSFloat64(ctx: JSContext, val: JSValue): JSResult[float64] = +proc fromJSFloat64(ctx: JSContext; val: JSValue): JSResult[float64] = var f64: float64 if JS_ToFloat64(ctx, addr f64, val) < 0: return err() @@ -150,14 +150,16 @@ macro fromJSTupleBody(a: tuple) = let doneVal = JS_GetProperty(ctx, next, ctx.getOpaque().str_refs[DONE]) `done` = ?fromJS[bool](ctx, doneVal) var i = `i` - # we're emulating a sequence, so we must query all remaining parameters too: + # we're emulating a sequence, so we must query all remaining parameters + # too: while not `done`: inc i let next = JS_Call(ctx, next_method, it, 0, nil) if JS_IsException(next): return err() defer: JS_FreeValue(ctx, next) - let doneVal = JS_GetProperty(ctx, next, ctx.getOpaque().str_refs[DONE]) + let doneVal = JS_GetProperty(ctx, next, + ctx.getOpaque().str_refs[DONE]) if JS_IsException(doneVal): return err() defer: JS_FreeValue(ctx, doneVal) @@ -166,10 +168,11 @@ macro fromJSTupleBody(a: tuple) = let msg = "Too many arguments in sequence (got " & $i & ", expected " & $`len` & ")" return err(newTypeError(msg)) - JS_FreeValue(ctx, JS_GetProperty(ctx, next, ctx.getOpaque().str_refs[VALUE])) + JS_FreeValue(ctx, JS_GetProperty(ctx, next, + ctx.getOpaque().str_refs[VALUE])) ) -proc fromJSTuple[T: tuple](ctx: JSContext, val: JSValue): JSResult[T] = +proc fromJSTuple[T: tuple](ctx: JSContext; val: JSValue): JSResult[T] = let itprop = JS_GetProperty(ctx, val, ctx.getOpaque().sym_refs[ITERATOR]) if JS_IsException(itprop): return err() @@ -186,7 +189,7 @@ proc fromJSTuple[T: tuple](ctx: JSContext, val: JSValue): JSResult[T] = fromJSTupleBody(x) return ok(x) -proc fromJSSeq[T](ctx: JSContext, val: JSValue): JSResult[seq[T]] = +proc fromJSSeq[T](ctx: JSContext; val: JSValue): JSResult[seq[T]] = let itprop = JS_GetProperty(ctx, val, ctx.getOpaque().sym_refs[ITERATOR]) if JS_IsException(itprop): return err() @@ -220,7 +223,7 @@ proc fromJSSeq[T](ctx: JSContext, val: JSValue): JSResult[seq[T]] = s.add(genericRes) return ok(s) -proc fromJSSet[T](ctx: JSContext, val: JSValue): JSResult[set[T]] = +proc fromJSSet[T](ctx: JSContext; val: JSValue): JSResult[set[T]] = let itprop = JS_GetProperty(ctx, val, ctx.getOpaque().sym_refs[ITERATOR]) if JS_IsException(itprop): return err() @@ -254,7 +257,7 @@ proc fromJSSet[T](ctx: JSContext, val: JSValue): JSResult[set[T]] = s.incl(genericRes) return ok(s) -proc fromJSTable[A, B](ctx: JSContext, val: JSValue): JSResult[Table[A, B]] = +proc fromJSTable[A, B](ctx: JSContext; val: JSValue): JSResult[Table[A, B]] = if not JS_IsObject(val): return err(newTypeError("object expected")) var ptab: ptr UncheckedArray[JSPropertyEnum] @@ -287,13 +290,13 @@ template optionType[T](o: type Option[T]): auto = # or null. (This is rather pointless for anything else.) # Opt is for passing down exceptions received up in the chain. # So e.g. none(T) translates to JS_NULL, but err() translates to JS_EXCEPTION. -proc fromJSOption[T](ctx: JSContext, val: JSValue): JSResult[Option[T]] = +proc fromJSOption[T](ctx: JSContext; val: JSValue): JSResult[Option[T]] = if JS_IsNull(val): return ok(none(T)) let res = ?fromJS[T](ctx, val) return ok(option(res)) -proc fromJSBool(ctx: JSContext, val: JSValue): JSResult[bool] = +proc fromJSBool(ctx: JSContext; val: JSValue): JSResult[bool] = let ret = JS_ToBool(ctx, val) if ret == -1: # exception return err() @@ -301,7 +304,7 @@ proc fromJSBool(ctx: JSContext, val: JSValue): JSResult[bool] = return ok(false) return ok(true) -proc fromJSEnum[T: enum](ctx: JSContext, val: JSValue): JSResult[T] = +proc fromJSEnum[T: enum](ctx: JSContext; val: JSValue): JSResult[T] = if JS_IsException(val): return err() let s = ?toString(ctx, val) @@ -310,7 +313,7 @@ proc fromJSEnum[T: enum](ctx: JSContext, val: JSValue): JSResult[T] = return ok(r.get) return errTypeError("`" & s & "' is not a valid value for enumeration " & $T) -proc fromJSPObj0(ctx: JSContext, val: JSValue, t: string): +proc fromJSPObj0(ctx: JSContext; val: JSValue; t: string): JSResult[pointer] = if JS_IsException(val): return err(nil) @@ -326,15 +329,15 @@ proc fromJSPObj0(ctx: JSContext, val: JSValue, t: string): let op = JS_GetOpaque(val, classid) return ok(op) -proc fromJSObject[T: ref object](ctx: JSContext, val: JSValue): JSResult[T] = +proc fromJSObject[T: ref object](ctx: JSContext; val: JSValue): JSResult[T] = return ok(cast[T](?fromJSPObj0(ctx, val, $T))) -proc fromJSVoid(ctx: JSContext, val: JSValue): JSResult[void] = +proc fromJSVoid(ctx: JSContext; val: JSValue): JSResult[void] = if JS_IsException(val): return err() return ok() -proc fromJSDict[T: JSDict](ctx: JSContext, val: JSValue): JSResult[T] = +proc fromJSDict[T: JSDict](ctx: JSContext; val: JSValue): JSResult[T] = if not JS_IsUndefined(val) and not JS_IsNull(val) and not JS_IsObject(val): return err(newTypeError("Dictionary is not an object")) #TODO throw on missing required values @@ -346,7 +349,7 @@ proc fromJSDict[T: JSDict](ctx: JSContext, val: JSValue): JSResult[T] = v = ?fromJS[typeof(v)](ctx, esm) return ok(d) -proc fromJSArrayBuffer(ctx: JSContext, val: JSValue): JSResult[JSArrayBuffer] = +proc fromJSArrayBuffer(ctx: JSContext; val: JSValue): JSResult[JSArrayBuffer] = var len: csize_t let p = JS_GetArrayBuffer(ctx, addr len, val) if p == nil: @@ -357,7 +360,7 @@ proc fromJSArrayBuffer(ctx: JSContext, val: JSValue): JSResult[JSArrayBuffer] = ) return ok(abuf) -proc fromJSArrayBufferView(ctx: JSContext, val: JSValue): +proc fromJSArrayBufferView(ctx: JSContext; val: JSValue): JSResult[JSArrayBufferView] = var offset: csize_t var nmemb: csize_t @@ -373,15 +376,15 @@ proc fromJSArrayBufferView(ctx: JSContext, val: JSValue): ) return ok(view) -proc promiseThenCallback(ctx: JSContext, this_val: JSValue, argc: cint, - argv: ptr JSValue, magic: cint, func_data: ptr JSValue): JSValue {.cdecl.} = +proc promiseThenCallback(ctx: JSContext; this_val: JSValue; argc: cint; + argv: ptr JSValue; magic: cint; func_data: ptr JSValue): JSValue {.cdecl.} = let op = JS_GetOpaque(func_data[], JS_GetClassID(func_data[])) let p = cast[EmptyPromise](op) p.resolve() GC_unref(p) return JS_UNDEFINED -proc fromJSEmptyPromise(ctx: JSContext, val: JSValue): JSResult[EmptyPromise] = +proc fromJSEmptyPromise(ctx: JSContext; val: JSValue): JSResult[EmptyPromise] = if not JS_IsObject(val): return err(newTypeError("Value is not an object")) #TODO I have a feeling this leaks memory in some cases :( @@ -403,7 +406,7 @@ macro fromJS2(ctx: JSContext; val: JSValue; x: static string): untyped = return quote do: `id`(`ctx`, `val`) -proc fromJS*[T](ctx: JSContext, val: JSValue): JSResult[T] = +proc fromJS*[T](ctx: JSContext; val: JSValue): JSResult[T] = when T is string: return fromJSString(ctx, val) elif T is Option: @@ -447,7 +450,7 @@ const JS_ATOM_TAG_INT = cuint(1u32 shl 31) func JS_IsNumber*(v: JSAtom): JS_BOOL = return (cast[cuint](v) and JS_ATOM_TAG_INT) != 0 -func fromJS*[T: string|uint32](ctx: JSContext, atom: JSAtom): Opt[T] = +func fromJS*[T: string|uint32](ctx: JSContext; atom: JSAtom): Opt[T] = when T is SomeNumber: if JS_IsNumber(atom): return ok(T(cast[uint32](atom) and (not JS_ATOM_TAG_INT))) @@ -455,10 +458,10 @@ func fromJS*[T: string|uint32](ctx: JSContext, atom: JSAtom): Opt[T] = let val = JS_AtomToValue(ctx, atom) return toString(ctx, val) -proc fromJSPObj[T](ctx: JSContext, val: JSValue): JSResult[ptr T] = +proc fromJSPObj[T](ctx: JSContext; val: JSValue): JSResult[ptr T] = return cast[JSResult[ptr T]](fromJSPObj0(ctx, val, $T)) -template fromJSP*[T](ctx: JSContext, val: JSValue): untyped = +template fromJSP*[T](ctx: JSContext; val: JSValue): untyped = when T is FromJSAllowedT: fromJSPObj[T](ctx, val) else: diff --git a/src/js/intl.nim b/src/js/intl.nim index 9e17edf3..8be5a768 100644 --- a/src/js/intl.nim +++ b/src/js/intl.nim @@ -17,8 +17,8 @@ jsDestructor(NumberFormat) jsDestructor(PluralRules) #TODO ...yeah -proc newNumberFormat(name: string = "en-US", - options = none(JSValue)): NumberFormat {.jsctor.} = +proc newNumberFormat(name: string = "en-US"; options = none(JSValue)): + NumberFormat {.jsctor.} = return NumberFormat() #TODO @@ -31,7 +31,7 @@ proc resolvedOptions(this: PluralRules): PRResolvedOptions {.jsfunc.} = ) #TODO: this should accept string/BigInt too -proc format(nf: NumberFormat, num: float64): string {.jsfunc.} = +proc format(nf: NumberFormat; num: float64): string {.jsfunc.} = let s = $num var i = 0 var L = s.len diff --git a/src/js/javascript.nim b/src/js/javascript.nim index ef848038..f2f72979 100644 --- a/src/js/javascript.nim +++ b/src/js/javascript.nim @@ -99,28 +99,28 @@ type ctorBody: NimNode BoundFunctionType = enum - FUNCTION = "js_func" - CONSTRUCTOR = "js_ctor" - GETTER = "js_get" - SETTER = "js_set" - PROPERTY_GET = "js_prop_get" - PROPERTY_SET = "js_prop_set" - PROPERTY_DEL = "js_prop_del" - PROPERTY_HAS = "js_prop_has" - PROPERTY_NAMES = "js_prop_names" - FINALIZER = "js_fin" + bfFunction = "js_func" + bfConstructor = "js_ctor" + bfGetter = "js_get" + bfSetter = "js_set" + bfPropertyGet = "js_prop_get" + bfPropertySet = "js_prop_set" + bfPropertyDel = "js_prop_del" + bfPropertyHas = "js_prop_has" + bfPropertyNames = "js_prop_names" + bfFinalizer = "js_fin" var runtimes {.threadvar.}: seq[JSRuntime] -proc bindMalloc(s: ptr JSMallocState, size: csize_t): pointer {.cdecl.} = +proc bindMalloc(s: ptr JSMallocState; size: csize_t): pointer {.cdecl.} = return alloc(size) -proc bindFree(s: ptr JSMallocState, p: pointer) {.cdecl.} = +proc bindFree(s: ptr JSMallocState; p: pointer) {.cdecl.} = if p == nil: return dealloc(p) -proc bindRealloc(s: ptr JSMallocState, p: pointer, size: csize_t): pointer +proc bindRealloc(s: ptr JSMallocState; p: pointer; size: csize_t): pointer {.cdecl.} = return realloc(p, size) @@ -147,16 +147,17 @@ proc newJSContext*(rt: JSRuntime): JSContext = JS_SetContextOpaque(ctx, cast[pointer](opaque)) return ctx -func getClass*(ctx: JSContext, class: string): JSClassID = +func getClass*(ctx: JSContext; class: string): JSClassID = # This function *should* never fail. ctx.getOpaque().creg[class] -func hasClass*(ctx: JSContext, class: type): bool = +func hasClass*(ctx: JSContext; class: type): bool = return $class in ctx.getOpaque().creg -func newJSCFunction*(ctx: JSContext, name: string, fun: JSCFunction, - argc: int = 0, proto = JS_CFUNC_generic, magic = 0): JSValue = - return JS_NewCFunction2(ctx, fun, cstring(name), cint(argc), proto, cint(magic)) +func newJSCFunction*(ctx: JSContext; name: string; fun: JSCFunction; + argc = 0; proto = JS_CFUNC_generic; magic = 0): JSValue = + return JS_NewCFunction2(ctx, fun, cstring(name), cint(argc), proto, + cint(magic)) proc free*(ctx: var JSContext) = var opaque = ctx.getOpaque() @@ -234,8 +235,8 @@ proc runJSJobs*(rt: JSRuntime; err: DynStream) = # Since every prototype has a list of all its ancestor's LegacyUnforgeable # functions, it is sufficient to simply merge the new list of new classes # with their parent's list to achieve this. -proc addClassUnforgeable(ctx: JSContext, proto: JSValue, - classid, parent: JSClassID, ourUnforgeable: JSFunctionList) = +proc addClassUnforgeable(ctx: JSContext; proto: JSValue; + classid, parent: JSClassID; ourUnforgeable: JSFunctionList) = let ctxOpaque = ctx.getOpaque() var merged = @ourUnforgeable ctxOpaque.unforgeable.withValue(parent, uf): @@ -245,18 +246,19 @@ proc addClassUnforgeable(ctx: JSContext, proto: JSValue, let ufp = addr ctxOpaque.unforgeable[classid][0] JS_SetPropertyFunctionList(ctx, proto, ufp, cint(merged.len)) -func newJSClass*(ctx: JSContext, cdef: JSClassDefConst, tname: string, - nimt: pointer, ctor: JSCFunction, funcs: JSFunctionList, parent: JSClassID, - asglobal: bool, nointerface: bool, finalizer: JSFinalizerFunction, - namespace: JSValue, errid: Opt[JSErrorEnum], - unforgeable, staticfuns: JSFunctionList, +func newJSClass*(ctx: JSContext; cdef: JSClassDefConst; tname: string; + nimt: pointer; ctor: JSCFunction; funcs: JSFunctionList; parent: JSClassID; + asglobal: bool; nointerface: bool; finalizer: JSFinalizerFunction; + namespace: JSValue; errid: Opt[JSErrorEnum]; + unforgeable, staticfuns: JSFunctionList; ishtmldda: bool): JSClassID {.discardable.} = let rt = JS_GetRuntime(ctx) discard JS_NewClassID(addr result) var ctxOpaque = ctx.getOpaque() var rtOpaque = rt.getOpaque() if JS_NewClass(rt, result, cdef) != 0: - raise newException(Defect, "Failed to allocate JS class: " & $cdef.class_name) + raise newException(Defect, "Failed to allocate JS class: " & + $cdef.class_name) ctxOpaque.typemap[nimt] = result ctxOpaque.creg[tname] = result ctxOpaque.parents[result] = parent @@ -276,7 +278,8 @@ func newJSClass*(ctx: JSContext, cdef: JSClassDefConst, tname: string, # (QuickJS uses the pointer later.) #TODO maybe put them in ctxOpaque instead? rtOpaque.flist.add(@funcs) - JS_SetPropertyFunctionList(ctx, proto, addr rtOpaque.flist[^1][0], cint(funcs.len)) + JS_SetPropertyFunctionList(ctx, proto, addr rtOpaque.flist[^1][0], + cint(funcs.len)) #TODO check if this is an indexed property getter if cdef.exotic != nil and cdef.exotic.get_own_property != nil: let val = JS_DupValue(ctx, ctxOpaque.Array_prototype_values) @@ -298,7 +301,8 @@ func newJSClass*(ctx: JSContext, cdef: JSClassDefConst, tname: string, ctxOpaque.unforgeable.withValue(result, uf): JS_SetPropertyFunctionList(ctx, global, addr uf[][0], cint(uf[].len)) JS_FreeValue(ctx, global) - let jctor = ctx.newJSCFunction($cdef.class_name, ctor, 0, JS_CFUNC_constructor) + let jctor = ctx.newJSCFunction($cdef.class_name, ctor, 0, + JS_CFUNC_constructor) if staticfuns.len > 0: rtOpaque.flist.add(@staticfuns) JS_SetPropertyFunctionList(ctx, jctor, addr rtOpaque.flist[^1][0], @@ -315,7 +319,11 @@ func newJSClass*(ctx: JSContext, cdef: JSClassDefConst, tname: string, else: ctx.definePropertyCW(namespace, $cdef.class_name, jctor) -type FuncParam = tuple[name: string, t: NimNode, val: Option[NimNode], generic: Option[NimNode]] +type FuncParam = tuple + name: string + t: NimNode + val: Option[NimNode] + generic: Option[NimNode] func getMinArgs(params: seq[FuncParam]): int = for i in 0..<params.len: @@ -329,17 +337,17 @@ func getMinArgs(params: seq[FuncParam]): int = return i return params.len -func fromJSP[T: string|uint32](ctx: JSContext, atom: JSAtom): Opt[T] = +func fromJSP[T: string|uint32](ctx: JSContext; atom: JSAtom): Opt[T] = return fromJS[T](ctx, atom) -proc defineConsts*[T](ctx: JSContext, classid: JSClassID, +proc defineConsts*[T](ctx: JSContext; classid: JSClassID; consts: static openArray[(string, T)]) = let proto = ctx.getOpaque().ctors[classid] for (k, v) in consts: ctx.definePropertyE(proto, k, v) -proc defineConsts*(ctx: JSContext, classid: JSClassID, - consts: typedesc[enum], astype: typedesc) = +proc defineConsts*(ctx: JSContext; classid: JSClassID; + consts: typedesc[enum]; astype: typedesc) = let proto = ctx.getOpaque().ctors[classid] for e in consts: ctx.definePropertyE(proto, $e, astype(e)) @@ -358,7 +366,8 @@ type newName: NimNode newBranchList: seq[NimNode] errval: NimNode # JS_EXCEPTION or -1 - dielabel: NimNode # die: didn't match parameters, but could still match other ones + # die: didn't match parameters, but could still match other ones + dielabel: NimNode jsFunCallLists: seq[NimNode] jsFunCallList: NimNode jsFunCall: NimNode @@ -400,8 +409,10 @@ proc getGenerics(fun: NimNode): Table[string, seq[NimNode]] = assert gen_name != nil gen_types.add(node) of nnkInfix: - assert node[0].eqIdent(ident("|")) or node[0].eqIdent(ident("or")), "Only OR generics are supported." - for i in countdown(node.len - 1, 1): stack.add(node[i]) # except infix ident + assert node[0].eqIdent(ident("|")) or node[0].eqIdent(ident("or")), + "Only OR generics are supported." + for i in countdown(node.len - 1, 1): # except infix ident + stack.add(node[i]) of nnkBracketExpr: gen_types.add(node) else: @@ -542,7 +553,8 @@ template fromJS_or_die*(t, ctx, val, ev, dl: untyped): untyped = break dl x.get -proc addParam2(gen: var JSFuncGenerator, s, t, val: NimNode, fallback: NimNode = nil) = +proc addParam2(gen: var JSFuncGenerator; s, t, val: NimNode; + fallback: NimNode = nil) = let ev = gen.errval let dl = gen.dielabel let stmt = quote do: @@ -558,11 +570,13 @@ proc addParam2(gen: var JSFuncGenerator, s, t, val: NimNode, fallback: NimNode = else: `fallback`)) -proc addValueParam(gen: var JSFuncGenerator, s, t: NimNode, fallback: NimNode = nil) = +proc addValueParam(gen: var JSFuncGenerator; s, t: NimNode; + fallback: NimNode = nil) = let j = gen.j gen.addParam2(s, t, quote do: argv[`j`], fallback) -proc addUnionParamBranch(gen: var JSFuncGenerator, query, newBranch: NimNode, fallback: NimNode = nil) = +proc addUnionParamBranch(gen: var JSFuncGenerator; query, newBranch: NimNode; + fallback: NimNode = nil) = let i = gen.i let query = if fallback == nil: query else: quote do: (`i` < argc and `query`) @@ -575,7 +589,7 @@ proc addUnionParamBranch(gen: var JSFuncGenerator, query, newBranch: NimNode, fa gen.jsFunCallLists[i] = oldBranch gen.newBranchList.add(newBranch) -func isSequence*(ctx: JSContext, o: JSValue): bool = +func isSequence*(ctx: JSContext; o: JSValue): bool = if not JS_IsObject(o): return false let prop = JS_GetProperty(ctx, o, ctx.getOpaque().sym_refs[ITERATOR]) @@ -583,7 +597,7 @@ func isSequence*(ctx: JSContext, o: JSValue): bool = result = not JS_IsUndefined(prop) JS_FreeValue(ctx, prop) -proc addUnionParam0(gen: var JSFuncGenerator, tt, s, val: NimNode, +proc addUnionParam0(gen: var JSFuncGenerator; tt, s, val: NimNode; fallback: NimNode = nil) = # Union types. #TODO quite a few types are still missing. @@ -695,12 +709,12 @@ proc addUnionParam0(gen: var JSFuncGenerator, tt, s, val: NimNode, gen.jsFunCallLists.add(branch) gen.newBranchList.setLen(0) -proc addUnionParam(gen: var JSFuncGenerator, tt: NimNode, s: NimNode, +proc addUnionParam(gen: var JSFuncGenerator; tt, s: NimNode; fallback: NimNode = nil) = let j = gen.j gen.addUnionParam0(tt, s, quote do: argv[`j`], fallback) -proc addFixParam(gen: var JSFuncGenerator, name: string) = +proc addFixParam(gen: var JSFuncGenerator; name: string) = let s = ident("arg_" & $gen.i) let t = gen.funcParams[gen.i][1] let id = ident(name) @@ -763,15 +777,15 @@ proc finishFunCallList(gen: var JSFuncGenerator) = var existingFuncs {.compileTime.}: HashSet[string] var jsDtors {.compileTime.}: HashSet[string] -proc registerFunction(typ: string, nf: BoundFunction) = +proc registerFunction(typ: string; nf: BoundFunction) = BoundFunctions.withValue(typ, val): val[].add(nf) do: BoundFunctions[typ] = @[nf] existingFuncs.incl(nf.id.strVal) -proc registerFunction(typ: string, t: BoundFunctionType, name: string, - id: NimNode, magic: uint16 = 0, uf = false, isstatic = false, +proc registerFunction(typ: string; t: BoundFunctionType; name: string; + id: NimNode; magic: uint16 = 0; uf = false; isstatic = false; ctorBody: NimNode = nil) = registerFunction(typ, BoundFunction( t: t, @@ -783,7 +797,7 @@ proc registerFunction(typ: string, t: BoundFunctionType, name: string, ctorBody: ctorBody )) -proc registerConstructor(gen: JSFuncGenerator, jsProc: NimNode) = +proc registerConstructor(gen: JSFuncGenerator; jsProc: NimNode) = registerFunction(gen.thisType, gen.t, gen.funcName, gen.newName, uf = gen.unforgeable, isstatic = gen.isstatic, ctorBody = jsProc) @@ -794,7 +808,7 @@ proc registerFunction(gen: JSFuncGenerator) = export JS_ThrowTypeError, JS_ThrowRangeError, JS_ThrowSyntaxError, JS_ThrowInternalError, JS_ThrowReferenceError -proc newJSProcBody(gen: var JSFuncGenerator, isva: bool): NimNode = +proc newJSProcBody(gen: var JSFuncGenerator; isva: bool): NimNode = let tt = gen.thisType let fn = gen.funcName let ma = gen.actualMinArgs @@ -817,13 +831,13 @@ proc newJSProcBody(gen: var JSFuncGenerator, isva: bool): NimNode = ) result.add(gen.jsCallAndRet) -proc newJSProc(gen: var JSFuncGenerator, params: openArray[NimNode], +proc newJSProc(gen: var JSFuncGenerator; params: openArray[NimNode]; isva = true): NimNode = let jsBody = gen.newJSProcBody(isva) let jsPragmas = newNimNode(nnkPragma).add(ident("cdecl")) return newProc(gen.newName, params, jsBody, pragmas = jsPragmas) -func getFuncName(fun: NimNode, jsname, staticName: string): string = +func getFuncName(fun: NimNode; jsname, staticName: string): string = if jsname != "": return jsname if staticName != "": @@ -837,8 +851,8 @@ func getFuncName(fun: NimNode, jsname, staticName: string): string = return x func getErrVal(t: BoundFunctionType): NimNode = - if t in {PROPERTY_GET, PROPERTY_SET, PROPERTY_DEL, PROPERTY_HAS, - PROPERTY_NAMES}: + if t in {bfPropertyGet, bfPropertySet, bfPropertyDel, bfPropertyHas, + bfPropertyNames}: return quote do: cint(-1) return quote do: JS_EXCEPTION @@ -851,7 +865,7 @@ proc addJSContext(gen: var JSFuncGenerator) = elif gen.funcParams[gen.i].t.eqIdent(ident("JSRuntime")): inc gen.i # special case for finalizers that have a JSRuntime param -proc addThisName(gen: var JSFuncGenerator, thisname: Option[string]) = +proc addThisName(gen: var JSFuncGenerator; thisname: Option[string]) = if thisname.isSome: gen.thisTypeNode = gen.funcParams[gen.i][1] gen.thisType = $gen.funcParams[gen.i][1] @@ -879,9 +893,8 @@ func getActualMinArgs(gen: var JSFuncGenerator): int = assert ma >= 0 return ma -proc initGenerator(fun: NimNode, t: BoundFunctionType, - thisname = some("this"), jsname: string = "", unforgeable = false, - staticName = ""): JSFuncGenerator = +proc initGenerator(fun: NimNode; t: BoundFunctionType; thisname = some("this"); + jsname = ""; unforgeable = false; staticName = ""): JSFuncGenerator = let jsFunCallList = newStmtList() let funcParams = getParams(fun) var gen = JSFuncGenerator( @@ -909,7 +922,7 @@ proc initGenerator(fun: NimNode, t: BoundFunctionType, gen.newName = ident($gen.t & "_" & gen.funcName) return gen -proc makeJSCallAndRet(gen: var JSFuncGenerator, okstmt, errstmt: NimNode) = +proc makeJSCallAndRet(gen: var JSFuncGenerator; okstmt, errstmt: NimNode) = let jfcl = gen.jsFunCallList let dl = gen.dielabel gen.jsCallAndRet = if gen.returnType.isSome: @@ -924,7 +937,7 @@ proc makeJSCallAndRet(gen: var JSFuncGenerator, okstmt, errstmt: NimNode) = `okstmt` `errstmt` -proc makeCtorJSCallAndRet(gen: var JSFuncGenerator, errstmt: NimNode) = +proc makeCtorJSCallAndRet(gen: var JSFuncGenerator; errstmt: NimNode) = let jfcl = gen.jsFunCallList let dl = gen.dielabel gen.jsCallAndRet = quote do: @@ -933,7 +946,7 @@ proc makeCtorJSCallAndRet(gen: var JSFuncGenerator, errstmt: NimNode) = `errstmt` macro jsctor*(fun: typed) = - var gen = initGenerator(fun, CONSTRUCTOR, thisname = none(string)) + var gen = initGenerator(fun, bfConstructor, thisname = none(string)) if gen.newName.strVal in existingFuncs: #TODO TODO TODO implement function overloading error("Function overloading hasn't been implemented yet...") @@ -948,7 +961,7 @@ macro jsctor*(fun: typed) = return newStmtList(fun) macro jshasprop*(fun: typed) = - var gen = initGenerator(fun, PROPERTY_HAS, thisname = some("obj")) + var gen = initGenerator(fun, bfPropertyHas, thisname = some("obj")) if gen.newName.strVal in existingFuncs: #TODO TODO TODO ditto error("Function overloading hasn't been implemented yet...") @@ -967,7 +980,7 @@ macro jshasprop*(fun: typed) = return newStmtList(fun, jsProc) macro jsgetprop*(fun: typed) = - var gen = initGenerator(fun, PROPERTY_GET, thisname = some("obj")) + var gen = initGenerator(fun, bfPropertyGet, thisname = some("obj")) if gen.newName.strVal in existingFuncs: #TODO TODO TODO ditto error("Function overloading hasn't been implemented yet...") @@ -996,7 +1009,7 @@ macro jsgetprop*(fun: typed) = return newStmtList(fun, jsProc) macro jssetprop*(fun: typed) = - var gen = initGenerator(fun, PROPERTY_SET, thisname = some("obj")) + var gen = initGenerator(fun, bfPropertySet, thisname = some("obj")) if gen.newName.strVal in existingFuncs: #TODO TODO TODO ditto error("Function overloading hasn't been implemented yet...") @@ -1024,7 +1037,7 @@ macro jssetprop*(fun: typed) = return newStmtList(fun, jsProc) macro jsdelprop*(fun: typed) = - var gen = initGenerator(fun, PROPERTY_DEL, thisname = some("obj")) + var gen = initGenerator(fun, bfPropertyDel, thisname = some("obj")) if gen.newName.strVal in existingFuncs: #TODO TODO TODO ditto error("Function overloading hasn't been implemented yet...") @@ -1043,7 +1056,7 @@ macro jsdelprop*(fun: typed) = return newStmtList(fun, jsProc) macro jspropnames*(fun: typed) = - var gen = initGenerator(fun, PROPERTY_NAMES, thisname = some("obj")) + var gen = initGenerator(fun, bfPropertyNames, thisname = some("obj")) if gen.newName.strVal in existingFuncs: #TODO TODO TODO ditto error("Function overloading hasn't been implemented yet...") @@ -1062,8 +1075,8 @@ macro jspropnames*(fun: typed) = gen.registerFunction() return newStmtList(fun, jsProc) -macro jsfgetn(jsname: static string, uf: static bool, fun: typed) = - var gen = initGenerator(fun, GETTER, jsname = jsname, unforgeable = uf) +macro jsfgetn(jsname: static string; uf: static bool; fun: typed) = + var gen = initGenerator(fun, bfGetter, jsname = jsname, unforgeable = uf) if gen.actualMinArgs != 0 or gen.funcParams.len != gen.minArgs: error("jsfget functions must only accept one parameter.") if gen.returnType.isNone: @@ -1085,16 +1098,16 @@ template jsfget*(fun: typed) = template jsuffget*(fun: typed) = jsfgetn("", true, fun) -template jsfget*(jsname: static string, fun: typed) = +template jsfget*(jsname: static string; fun: typed) = jsfgetn(jsname, false, fun) -template jsuffget*(jsname: static string, fun: typed) = +template jsuffget*(jsname: static string; fun: typed) = jsfgetn(jsname, true, fun) # Ideally we could simulate JS setters using nim setters, but nim setters # won't accept types that don't match their reflected field's type. -macro jsfsetn(jsname: static string, fun: typed) = - var gen = initGenerator(fun, SETTER, jsname = jsname) +macro jsfsetn(jsname: static string; fun: typed) = + var gen = initGenerator(fun, bfSetter, jsname = jsname) if gen.actualMinArgs != 1 or gen.funcParams.len != gen.minArgs: error("jsfset functions must accept two parameters") if gen.returnType.isSome: @@ -1118,15 +1131,16 @@ macro jsfsetn(jsname: static string, fun: typed) = template jsfset*(fun: typed) = jsfsetn("", fun) -template jsfset*(jsname: static string, fun: typed) = +template jsfset*(jsname: static string; fun: typed) = jsfsetn(jsname, fun) -macro jsfuncn*(jsname: static string, uf: static bool, - staticName: static string, fun: typed) = - var gen = initGenerator(fun, FUNCTION, jsname = jsname, unforgeable = uf, +macro jsfuncn*(jsname: static string; uf: static bool; + staticName: static string; fun: typed) = + var gen = initGenerator(fun, bfFunction, jsname = jsname, unforgeable = uf, staticName = staticName) if gen.minArgs == 0 and not gen.isstatic: - error("Zero-parameter functions are not supported. (Maybe pass Window or Client?)") + error("Zero-parameter functions are not supported. " & + "(Maybe pass Window or Client?)") if not gen.isstatic: gen.addFixParam("this") gen.addRequiredParams() @@ -1147,23 +1161,23 @@ template jsfunc*(fun: typed) = template jsuffunc*(fun: typed) = jsfuncn("", true, "", fun) -template jsfunc*(jsname: static string, fun: typed) = +template jsfunc*(jsname: static string; fun: typed) = jsfuncn(jsname, false, "", fun) -template jsuffunc*(jsname: static string, fun: typed) = +template jsuffunc*(jsname: static string; fun: typed) = jsfuncn(jsname, true, "", fun) -template jsstfunc*(name: static string, fun: typed) = +template jsstfunc*(name: static string; fun: typed) = jsfuncn("", false, name, fun) macro jsfin*(fun: typed) = - var gen = initGenerator(fun, FINALIZER, thisname = some("fin")) + var gen = initGenerator(fun, bfFinalizer, thisname = some("fin")) let finName = gen.newName let finFun = ident(gen.funcName) let t = gen.thisTypeNode if gen.minArgs == 1: let jsProc = quote do: - proc `finName`(rt: JSRuntime, val: JSValue) = + proc `finName`(rt: JSRuntime; val: JSValue) = let opaque = JS_GetOpaque(val, JS_GetClassID(val)) if opaque != nil: `finFun`(cast[`t`](opaque)) @@ -1171,7 +1185,7 @@ macro jsfin*(fun: typed) = result = newStmtList(fun, jsProc) elif gen.minArgs == 2: let jsProc = quote do: - proc `finName`(rt: JSRuntime, val: JSValue) = + proc `finName`(rt: JSRuntime; val: JSValue) = let opaque = JS_GetOpaque(val, JS_GetClassID(val)) if opaque != nil: `finFun`(rt, cast[`t`](opaque)) @@ -1191,7 +1205,7 @@ template jsgetset*(name: string) {.pragma.} template jsufget*() {.pragma.} template jsufget*(name: string) {.pragma.} -proc js_illegal_ctor*(ctx: JSContext, this: JSValue, argc: cint, +proc js_illegal_ctor*(ctx: JSContext; this: JSValue; argc: cint; argv: ptr UncheckedArray[JSValue]): JSValue {.cdecl.} = return JS_ThrowTypeError(ctx, "Illegal constructor") @@ -1339,7 +1353,7 @@ func jsname(info: RegistryInfo): string = return info.name return info.tname -proc newRegistryInfo(t: NimNode, name: string): RegistryInfo = +proc newRegistryInfo(t: NimNode; name: string): RegistryInfo = let info = RegistryInfo( t: t, name: name, @@ -1360,13 +1374,13 @@ proc newRegistryInfo(t: NimNode, name: string): RegistryInfo = warning("No destructor has been defined for type " & info.tname) return info -proc bindConstructor(stmts: NimNode, info: var RegistryInfo): NimNode = +proc bindConstructor(stmts: NimNode; info: var RegistryInfo): NimNode = if info.ctorFun != nil: stmts.add(info.ctorImpl) return info.ctorFun return ident("js_illegal_ctor") -proc registerGetters(stmts: NimNode, info: RegistryInfo, +proc registerGetters(stmts: NimNode; info: RegistryInfo; jsget: seq[JSObjectPragma]) = let t = info.t let tname = info.tname @@ -1374,9 +1388,9 @@ proc registerGetters(stmts: NimNode, info: RegistryInfo, for op in jsget: let node = op.varsym let fn = op.name - let id = ident($GETTER & "_" & tname & "_" & fn) + let id = ident($bfGetter & "_" & tname & "_" & fn) stmts.add(quote do: - proc `id`(ctx: JSContext, this: JSValue): JSValue {.cdecl.} = + proc `id`(ctx: JSContext; this: JSValue): JSValue {.cdecl.} = if not ctx.isInstanceOf(this, `tname`): return JS_ThrowTypeError(ctx, "'%s' called on an object that is not an instance of %s", `fn`, @@ -1388,13 +1402,13 @@ proc registerGetters(stmts: NimNode, info: RegistryInfo, return toJS(ctx, arg_0.`node`) ) registerFunction(tname, BoundFunction( - t: GETTER, + t: bfGetter, name: fn, id: id, unforgeable: op.unforgeable )) -proc registerSetters(stmts: NimNode, info: RegistryInfo, +proc registerSetters(stmts: NimNode; info: RegistryInfo; jsset: seq[JSObjectPragma]) = let t = info.t let tname = info.tname @@ -1402,9 +1416,9 @@ proc registerSetters(stmts: NimNode, info: RegistryInfo, for op in jsset: let node = op.varsym let fn = op.name - let id = ident($SETTER & "_" & tname & "_" & fn) + let id = ident($bfSetter & "_" & tname & "_" & fn) stmts.add(quote do: - proc `id`(ctx: JSContext, this: JSValue, val: JSValue): JSValue + proc `id`(ctx: JSContext; this: JSValue; val: JSValue): JSValue {.cdecl.} = if not ctx.isInstanceOf(this, `tname`): return JS_ThrowTypeError(ctx, @@ -1417,9 +1431,9 @@ proc registerSetters(stmts: NimNode, info: RegistryInfo, arg_0.`node` = fromJS_or_return(typeof(arg_0.`node`), ctx, arg_1) return JS_DupValue(ctx, arg_1) ) - registerFunction(tname, SETTER, fn, id) + registerFunction(tname, bfSetter, fn, id) -proc bindFunctions(stmts: NimNode, info: var RegistryInfo) = +proc bindFunctions(stmts: NimNode; info: var RegistryInfo) = BoundFunctions.withValue(info.tname, funs): for fun in funs[].mitems: var f0 = fun.name @@ -1427,7 +1441,7 @@ proc bindFunctions(stmts: NimNode, info: var RegistryInfo) = if fun.name.endsWith("_exceptions"): fun.name = fun.name.substr(0, fun.name.high - "_exceptions".len) case fun.t - of FUNCTION: + of bfFunction: f0 = fun.name if fun.unforgeable: info.tabUnforgeable.add(quote do: @@ -1438,48 +1452,48 @@ proc bindFunctions(stmts: NimNode, info: var RegistryInfo) = else: info.tabList.add(quote do: JS_CFUNC_DEF(`f0`, 0, cast[JSCFunction](`f1`))) - of CONSTRUCTOR: + of bfConstructor: info.ctorImpl = fun.ctorBody if info.ctorFun != nil: error("Class " & info.tname & " has 2+ constructors.") info.ctorFun = f1 - of GETTER: + of bfGetter: info.getset.withValue(f0, exv): exv[0] = f1 exv[2] = fun.unforgeable do: info.getset[f0] = (f1, newNilLit(), fun.unforgeable) - of SETTER: + of bfSetter: info.getset.withValue(f0, exv): exv[1] = f1 do: info.getset[f0] = (newNilLit(), f1, false) - of PROPERTY_GET: + of bfPropertyGet: if info.propGetFun.kind != nnkNilLit: error("Class " & info.tname & " has 2+ property getters.") info.propGetFun = f1 - of PROPERTY_SET: + of bfPropertySet: if info.propSetFun.kind != nnkNilLit: error("Class " & info.tname & " has 2+ property setters.") info.propSetFun = f1 - of PROPERTY_DEL: + of bfPropertyDel: if info.propDelFun.kind != nnkNilLit: error("Class " & info.tname & " has 2+ property setters.") info.propDelFun = f1 - of PROPERTY_HAS: + of bfPropertyHas: if info.propHasFun.kind != nnkNilLit: error("Class " & info.tname & " has 2+ hasprop getters.") info.propHasFun = f1 - of PROPERTY_NAMES: + of bfPropertyNames: if info.propNamesFun.kind != nnkNilLit: error("Class " & info.tname & " has 2+ propnames getters.") info.propNamesFun = f1 - of FINALIZER: + of bfFinalizer: f0 = fun.name info.finFun = ident(f0) info.finName = f1 -proc bindGetSet(stmts: NimNode, info: RegistryInfo) = +proc bindGetSet(stmts: NimNode; info: RegistryInfo) = for k, (get, set, unforgeable) in info.getset: if not unforgeable: info.tabList.add(quote do: JS_CGETSET_DEF(`k`, `get`, `set`)) @@ -1487,7 +1501,7 @@ proc bindGetSet(stmts: NimNode, info: RegistryInfo) = info.tabUnforgeable.add(quote do: JS_CGETSET_DEF_NOCONF(`k`, `get`, `set`)) -proc bindExtraGetSet(stmts: NimNode, info: var RegistryInfo, +proc bindExtraGetSet(stmts: NimNode; info: var RegistryInfo; extra_getset: openArray[TabGetSet]) = for x in extra_getset: let k = x.name @@ -1496,11 +1510,11 @@ proc bindExtraGetSet(stmts: NimNode, info: var RegistryInfo, let m = x.magic info.tabList.add(quote do: JS_CGETSET_MAGIC_DEF(`k`, `g`, `s`, `m`)) -proc bindCheckDestroy(stmts: NimNode, info: RegistryInfo) = +proc bindCheckDestroy(stmts: NimNode; info: RegistryInfo) = let t = info.t let dfin = info.dfin stmts.add(quote do: - proc `dfin`(rt: JSRuntime, val: JSValue): JS_BOOL {.cdecl.} = + proc `dfin`(rt: JSRuntime; val: JSValue): JS_BOOL {.cdecl.} = let opaque = JS_GetOpaque(val, JS_GetClassID(val)) if opaque != nil: when `t` is ref object: @@ -1549,7 +1563,7 @@ proc bindCheckDestroy(stmts: NimNode, info: RegistryInfo) = return true ) -proc bindEndStmts(endstmts: NimNode, info: RegistryInfo) = +proc bindEndStmts(endstmts: NimNode; info: RegistryInfo) = let jsname = info.jsname let dfin = info.dfin let classDef = info.classDef @@ -1586,7 +1600,7 @@ proc bindEndStmts(endstmts: NimNode, info: RegistryInfo) = ) let `classDef` = JSClassDefConst(addr cd)) -macro registerType*(ctx: typed, t: typed, parent: JSClassID = 0, +macro registerType*(ctx: typed; t: typed; parent: JSClassID = 0, asglobal = false, nointerface = false, name: static string = "", has_extra_getset: static bool = false, extra_getset: static openArray[TabGetSet] = [], @@ -1625,7 +1639,7 @@ macro registerType*(ctx: typed, t: typed, parent: JSClassID = 0, proc getMemoryUsage*(rt: JSRuntime): string = var m: JSMemoryUsage JS_ComputeMemoryUsage(rt, addr m) - template row(title: string, count, size, sz2, cnt2: int64, name: string): + template row(title: string; count, size, sz2, cnt2: int64, name: string): string = let fv0 = $(float(sz2) / float(cnt2)) var fv = fv0.until('.') @@ -1634,10 +1648,10 @@ proc getMemoryUsage*(rt: JSRuntime): string = else: fv &= ".0" title & ": " & $count & " " & $size & " (" & fv & ")" & name & "\n" - template row(title: string, count, size, sz2: int64, name: string): + template row(title: string; count, size, sz2: int64, name: string): string = row(title, count, size, sz2, count, name) - template row(title: string, count, size: int64, name: string): string = + template row(title: string; count, size: int64, name: string): string = row(title, count, size, size, name) var s = "" if m.malloc_count != 0: @@ -1668,17 +1682,17 @@ proc getMemoryUsage*(rt: JSRuntime): string = $m.binary_object_size return s -proc eval*(ctx: JSContext, s: string, file: string, eval_flags: int): JSValue = +proc eval*(ctx: JSContext; s: string; file: string; eval_flags: int): JSValue = return JS_Eval(ctx, cstring(s), csize_t(s.len), cstring(file), cint(eval_flags)) -proc compileScript*(ctx: JSContext, s: string, file: cstring): JSValue = +proc compileScript*(ctx: JSContext; s: string; file: cstring): JSValue = return JS_Eval(ctx, cstring(s), csize_t(s.len), file, cint(JS_EVAL_FLAG_COMPILE_ONLY)) -proc compileModule*(ctx: JSContext, s: string, file: cstring): JSValue = +proc compileModule*(ctx: JSContext; s: string; file: cstring): JSValue = return JS_Eval(ctx, cstring(s), csize_t(s.len), file, cint(JS_EVAL_TYPE_MODULE or JS_EVAL_FLAG_COMPILE_ONLY)) -proc evalFunction*(ctx: JSContext, val: JSValue): JSValue = +proc evalFunction*(ctx: JSContext; val: JSValue): JSValue = return JS_EvalFunction(ctx, val) diff --git a/src/js/module.nim b/src/js/module.nim index fe080f13..2e15ead3 100644 --- a/src/js/module.nim +++ b/src/js/module.nim @@ -3,7 +3,7 @@ import bindings/quickjs import js/javascript import js/tojs -proc setImportMeta(ctx: JSContext, funcVal: JSValue, isMain: bool) = +proc setImportMeta(ctx: JSContext; funcVal: JSValue; isMain: bool) = let m = cast[JSModuleDef](JS_VALUE_GET_PTR(funcVal)) let moduleNameAtom = JS_GetModuleName(ctx, m) let moduleName = JS_AtomToCString(ctx, moduleNameAtom) @@ -14,7 +14,7 @@ proc setImportMeta(ctx: JSContext, funcVal: JSValue, isMain: bool) = JS_FreeAtom(ctx, moduleNameAtom) JS_FreeCString(ctx, moduleName) -proc finishLoadModule*(ctx: JSContext, f: string, name: cstring): JSModuleDef = +proc finishLoadModule*(ctx: JSContext; f: string; name: cstring): JSModuleDef = let funcVal = compileModule(ctx, f, name) if JS_IsException(funcVal): return nil @@ -24,6 +24,6 @@ proc finishLoadModule*(ctx: JSContext, f: string, name: cstring): JSModuleDef = result = cast[JSModuleDef](JS_VALUE_GET_PTR(funcVal)) JS_FreeValue(ctx, funcVal) -proc normalizeModuleName*(ctx: JSContext, base_name, name: cstringConst, +proc normalizeModuleName*(ctx: JSContext; base_name, name: cstringConst; opaque: pointer): cstring {.cdecl.} = return js_strdup(ctx, cstring(name)) diff --git a/src/js/opaque.nim b/src/js/opaque.nim index bff250d0..490747bc 100644 --- a/src/js/opaque.nim +++ b/src/js/opaque.nim @@ -38,7 +38,7 @@ type err_ctors*: array[JSErrorEnum, JSValue] htmldda*: JSClassID # only one of these exists: document.all. - JSFinalizerFunction* = proc(rt: JSRuntime, val: JSValue) {.nimcall.} + JSFinalizerFunction* = proc(rt: JSRuntime; val: JSValue) {.nimcall.} JSRuntimeOpaque* = ref object plist*: Table[pointer, pointer] # Nim, JS @@ -70,7 +70,8 @@ func newJSContextOpaque*(ctx: JSContext): JSContextOpaque = JS_FreeValue(ctx, arrproto) block: let objproto = JS_GetClassProto(ctx, JS_CLASS_OBJECT) - opaque.Object_prototype_valueOf = JS_GetPropertyStr(ctx, objproto, "valueOf") + opaque.Object_prototype_valueOf = JS_GetPropertyStr(ctx, objproto, + "valueOf") JS_FreeValue(ctx, objproto) block: opaque.Uint8Array_ctor = JS_GetPropertyStr(ctx, global, "Uint8Array") @@ -94,11 +95,11 @@ func getOpaque*(ctx: JSContext): JSContextOpaque = func getOpaque*(rt: JSRuntime): JSRuntimeOpaque = return cast[JSRuntimeOpaque](JS_GetRuntimeOpaque(rt)) -func isGlobal*(ctx: JSContext, class: string): bool = +func isGlobal*(ctx: JSContext; class: string): bool = assert class != "" return ctx.getOpaque().gclaz == class -proc setOpaque*(ctx: JSContext, val: JSValue, opaque: pointer) = +proc setOpaque*(ctx: JSContext; val: JSValue; opaque: pointer) = let rt = JS_GetRuntime(ctx) let rtOpaque = rt.getOpaque() let p = JS_VALUE_GET_PTR(val) @@ -110,8 +111,7 @@ func getOpaque0*(val: JSValue): pointer = if JS_VALUE_GET_TAG(val) == JS_TAG_OBJECT: return JS_GetOpaque(val, JS_GetClassID(val)) -func getGlobalOpaque0*(ctx: JSContext, val: JSValue = JS_UNDEFINED): - Opt[pointer] = +func getGlobalOpaque0*(ctx: JSContext; val = JS_UNDEFINED): Opt[pointer] = let global = JS_GetGlobalObject(ctx) if JS_IsUndefined(val) or val == global: let opaque = JS_GetOpaque(global, JS_CLASS_OBJECT) @@ -120,10 +120,10 @@ func getGlobalOpaque0*(ctx: JSContext, val: JSValue = JS_UNDEFINED): JS_FreeValue(ctx, global) return err() -func getGlobalOpaque*(ctx: JSContext, T: typedesc, val: JSValue = JS_UNDEFINED): Opt[T] = +func getGlobalOpaque*(ctx: JSContext; T: typedesc; val = JS_UNDEFINED): Opt[T] = return ok(cast[T](?getGlobalOpaque0(ctx, val))) -func getOpaque*(ctx: JSContext, val: JSValue, class: string): pointer = +func getOpaque*(ctx: JSContext; val: JSValue; class: string): pointer = # Unfortunately, we can't change the global object's class. #TODO: or maybe we can, but I'm afraid of breaking something. # This needs further investigation. @@ -134,5 +134,5 @@ func getOpaque*(ctx: JSContext, val: JSValue, class: string): pointer = return opaque return getOpaque0(val) -func getOpaque*[T: ref object](ctx: JSContext, val: JSValue): T = +func getOpaque*[T: ref object](ctx: JSContext; val: JSValue): T = cast[T](getOpaque(ctx, val, $T)) diff --git a/src/js/propertyenumlist.nim b/src/js/propertyenumlist.nim index 98ad2a4f..65c16c2f 100644 --- a/src/js/propertyenumlist.nim +++ b/src/js/propertyenumlist.nim @@ -13,7 +13,7 @@ type is_enumerable: bool name: string -func newJSPropertyEnumList*(ctx: JSContext, size: uint32): JSPropertyEnumList = +func newJSPropertyEnumList*(ctx: JSContext; size: uint32): JSPropertyEnumList = let p = js_malloc(ctx, csize_t(sizeof(JSPropertyEnum)) * csize_t(size)) let buffer = cast[JSPropertyEnumArray](p) return JSPropertyEnumList( @@ -27,14 +27,14 @@ proc grow(this: var JSPropertyEnumList) = let p = js_realloc(this.ctx, this.buffer, csize_t(this.size)) this.buffer = cast[JSPropertyEnumArray](p) -proc add*(this: var JSPropertyEnumList, val: uint32) = +proc add*(this: var JSPropertyEnumList; val: uint32) = let i = this.len inc this.len if this.size < this.len: this.grow() this.buffer[i].atom = JS_NewAtomUInt32(this.ctx, val) -proc add*(this: var JSPropertyEnumList, val: string) = +proc add*(this: var JSPropertyEnumList; val: string) = let i = this.len inc this.len if this.size < this.len: diff --git a/src/js/regex.nim b/src/js/regex.nim index e189b430..f24633d1 100644 --- a/src/js/regex.nim +++ b/src/js/regex.nim @@ -49,7 +49,7 @@ proc compileRegex*(buf: string; flags: LREFlags = {}): Result[Regex, string] = ) return ok(regex) -func countBackslashes(buf: string, i: int): int = +func countBackslashes(buf: string; i: int): int = var j = 0 for i in countdown(i, 0): if buf[i] != '\\': diff --git a/src/js/tojs.nim b/src/js/tojs.nim index 807c85f5..4cbd2045 100644 --- a/src/js/tojs.nim +++ b/src/js/tojs.nim @@ -50,33 +50,33 @@ import types/opt # Convert Nim types to the corresponding JavaScript type. # This does not work with var objects. -proc toJS*(ctx: JSContext, s: string): JSValue -proc toJS*(ctx: JSContext, r: Rune): JSValue -proc toJS*(ctx: JSContext, n: int64): JSValue -proc toJS*(ctx: JSContext, n: int32): JSValue -proc toJS*(ctx: JSContext, n: int): JSValue -proc toJS*(ctx: JSContext, n: uint16): JSValue -proc toJS*(ctx: JSContext, n: uint32): JSValue -proc toJS*(ctx: JSContext, n: uint64): JSValue -proc toJS*(ctx: JSContext, n: float64): JSValue -proc toJS*(ctx: JSContext, b: bool): JSValue -proc toJS*[U, V](ctx: JSContext, t: Table[U, V]): JSValue -proc toJS*(ctx: JSContext, opt: Option): JSValue -proc toJS*[T, E](ctx: JSContext, opt: Result[T, E]): JSValue -proc toJS*(ctx: JSContext, s: seq): JSValue -proc toJS*[T](ctx: JSContext, s: set[T]): JSValue -proc toJS*(ctx: JSContext, t: tuple): JSValue -proc toJS*(ctx: JSContext, e: enum): JSValue -proc toJS*(ctx: JSContext, j: JSValue): JSValue -proc toJS*[T](ctx: JSContext, promise: Promise[T]): JSValue -proc toJS*[T, E](ctx: JSContext, promise: Promise[Result[T, E]]): JSValue -proc toJS*(ctx: JSContext, promise: EmptyPromise): JSValue -proc toJS*(ctx: JSContext, obj: ref object): JSValue -proc toJS*(ctx: JSContext, err: JSError): JSValue -proc toJS*(ctx: JSContext, abuf: JSArrayBuffer): JSValue -proc toJS*(ctx: JSContext, u8a: JSUint8Array): JSValue -proc toJS*(ctx: JSContext, ns: NarrowString): JSValue -proc toJS*(ctx: JSContext, dict: JSDict): JSValue +proc toJS*(ctx: JSContext; s: string): JSValue +proc toJS*(ctx: JSContext; r: Rune): JSValue +proc toJS*(ctx: JSContext; n: int64): JSValue +proc toJS*(ctx: JSContext; n: int32): JSValue +proc toJS*(ctx: JSContext; n: int): JSValue +proc toJS*(ctx: JSContext; n: uint16): JSValue +proc toJS*(ctx: JSContext; n: uint32): JSValue +proc toJS*(ctx: JSContext; n: uint64): JSValue +proc toJS*(ctx: JSContext; n: float64): JSValue +proc toJS*(ctx: JSContext; b: bool): JSValue +proc toJS*[U, V](ctx: JSContext; t: Table[U, V]): JSValue +proc toJS*(ctx: JSContext; opt: Option): JSValue +proc toJS*[T, E](ctx: JSContext; opt: Result[T, E]): JSValue +proc toJS*(ctx: JSContext; s: seq): JSValue +proc toJS*[T](ctx: JSContext; s: set[T]): JSValue +proc toJS*(ctx: JSContext; t: tuple): JSValue +proc toJS*(ctx: JSContext; e: enum): JSValue +proc toJS*(ctx: JSContext; j: JSValue): JSValue +proc toJS*[T](ctx: JSContext; promise: Promise[T]): JSValue +proc toJS*[T, E](ctx: JSContext; promise: Promise[Result[T, E]]): JSValue +proc toJS*(ctx: JSContext; promise: EmptyPromise): JSValue +proc toJS*(ctx: JSContext; obj: ref object): JSValue +proc toJS*(ctx: JSContext; err: JSError): JSValue +proc toJS*(ctx: JSContext; abuf: JSArrayBuffer): JSValue +proc toJS*(ctx: JSContext; u8a: JSUint8Array): JSValue +proc toJS*(ctx: JSContext; ns: NarrowString): JSValue +proc toJS*(ctx: JSContext; dict: JSDict): JSValue # Convert Nim types to the corresponding JavaScript type, with knowledge of # the parent object. @@ -85,20 +85,20 @@ proc toJS*(ctx: JSContext, dict: JSDict): JSValue # The idea here is to allow conversion of var objects to quasi-reference types # by saving a pointer to their ancestor and incrementing/decrementing the # ancestor's reference count instead. -proc toJSP*(ctx: JSContext, parent: ref object, child: var object): JSValue -proc toJSP*(ctx: JSContext, parent: ptr object, child: var object): JSValue +proc toJSP*(ctx: JSContext; parent: ref object; child: var object): JSValue +proc toJSP*(ctx: JSContext; parent: ptr object; child: var object): JSValue # Same as toJS, but used in constructors. ctor contains the target prototype, # used for subclassing from JS. -proc toJSNew*(ctx: JSContext, obj: ref object, ctor: JSValue): JSValue -proc toJSNew*[T, E](ctx: JSContext, opt: Result[T, E], ctor: JSValue): JSValue +proc toJSNew*(ctx: JSContext; obj: ref object; ctor: JSValue): JSValue +proc toJSNew*[T, E](ctx: JSContext; opt: Result[T, E]; ctor: JSValue): JSValue # Avoid accidentally calling toJSP on objects that we have explicit toJS # converters for. template makeToJSP(typ: untyped) = - template toJSP*(ctx: JSContext, parent: ref object, child: var typ): JSValue = + template toJSP*(ctx: JSContext; parent: ref object; child: var typ): JSValue = toJS(ctx, child) - template toJSP*(ctx: JSContext, parent: ptr object, child: var typ): JSValue = + template toJSP*(ctx: JSContext; parent: ptr object; child: var typ): JSValue = toJS(ctx, child) makeToJSP(Table) makeToJSP(Option) @@ -106,42 +106,42 @@ makeToJSP(Result) makeToJSP(JSValue) makeToJSP(JSDict) -proc defineProperty(ctx: JSContext, this: JSValue, name: JSAtom, - prop: JSValue, flags = cint(0)) = +proc defineProperty(ctx: JSContext; this: JSValue; name: JSAtom; + prop: JSValue; flags = cint(0)) = if JS_DefinePropertyValue(ctx, this, name, prop, flags) <= 0: raise newException(Defect, "Failed to define property string") -proc definePropertyC*(ctx: JSContext, this: JSValue, name: JSAtom, +proc definePropertyC*(ctx: JSContext; this: JSValue; name: JSAtom; prop: JSValue) = ctx.defineProperty(this, name, prop, JS_PROP_CONFIGURABLE) -proc defineProperty(ctx: JSContext, this: JSValue, name: string, - prop: JSValue, flags = cint(0)) = +proc defineProperty(ctx: JSContext; this: JSValue; name: string; + prop: JSValue; flags = cint(0)) = if JS_DefinePropertyValueStr(ctx, this, cstring(name), prop, flags) <= 0: raise newException(Defect, "Failed to define property string: " & name) -proc definePropertyC*(ctx: JSContext, this: JSValue, name: string, +proc definePropertyC*(ctx: JSContext; this: JSValue; name: string; prop: JSValue) = ctx.defineProperty(this, name, prop, JS_PROP_CONFIGURABLE) -proc defineProperty*[T](ctx: JSContext, this: JSValue, name: string, prop: T, +proc defineProperty*[T](ctx: JSContext; this: JSValue; name: string; prop: T; flags = cint(0)) = defineProperty(ctx, this, name, toJS(ctx, prop), flags) -proc definePropertyE*[T](ctx: JSContext, this: JSValue, name: string, +proc definePropertyE*[T](ctx: JSContext; this: JSValue; name: string; prop: T) = defineProperty(ctx, this, name, prop, JS_PROP_ENUMERABLE) -proc definePropertyCW*[T](ctx: JSContext, this: JSValue, name: string, +proc definePropertyCW*[T](ctx: JSContext; this: JSValue; name: string; prop: T) = defineProperty(ctx, this, name, prop, JS_PROP_CONFIGURABLE or JS_PROP_WRITABLE) -proc definePropertyCWE*[T](ctx: JSContext, this: JSValue, name: string, +proc definePropertyCWE*[T](ctx: JSContext; this: JSValue; name: string; prop: T) = defineProperty(ctx, this, name, prop, JS_PROP_C_W_E) -proc newFunction*(ctx: JSContext, args: openArray[string], body: string): +proc newFunction*(ctx: JSContext; args: openArray[string]; body: string): JSValue = var paramList: seq[JSValue] = @[] for arg in args: @@ -153,54 +153,54 @@ proc newFunction*(ctx: JSContext, args: openArray[string], body: string): JS_FreeValue(ctx, param) return fun -proc toJS*(ctx: JSContext, s: cstring): JSValue = +proc toJS*(ctx: JSContext; s: cstring): JSValue = return JS_NewString(ctx, s) -proc toJS*(ctx: JSContext, s: string): JSValue = +proc toJS*(ctx: JSContext; s: string): JSValue = return toJS(ctx, cstring(s)) -proc toJS*(ctx: JSContext, r: Rune): JSValue = +proc toJS*(ctx: JSContext; r: Rune): JSValue = return toJS(ctx, $r) -proc toJS*(ctx: JSContext, n: int32): JSValue = +proc toJS*(ctx: JSContext; n: int32): JSValue = return JS_NewInt32(ctx, n) -proc toJS*(ctx: JSContext, n: int64): JSValue = +proc toJS*(ctx: JSContext; n: int64): JSValue = return JS_NewInt64(ctx, n) # Always int32, so we don't risk 32-bit only breakage. -proc toJS*(ctx: JSContext, n: int): JSValue = +proc toJS*(ctx: JSContext; n: int): JSValue = return toJS(ctx, int32(n)) -proc toJS*(ctx: JSContext, n: uint16): JSValue = +proc toJS*(ctx: JSContext; n: uint16): JSValue = return JS_NewUint32(ctx, uint32(n)) -proc toJS*(ctx: JSContext, n: uint32): JSValue = +proc toJS*(ctx: JSContext; n: uint32): JSValue = return JS_NewUint32(ctx, n) -proc toJS*(ctx: JSContext, n: uint64): JSValue = +proc toJS*(ctx: JSContext; n: uint64): JSValue = #TODO this is incorrect return JS_NewFloat64(ctx, float64(n)) -proc toJS*(ctx: JSContext, n: float64): JSValue = +proc toJS*(ctx: JSContext; n: float64): JSValue = return JS_NewFloat64(ctx, n) -proc toJS*(ctx: JSContext, b: bool): JSValue = +proc toJS*(ctx: JSContext; b: bool): JSValue = return JS_NewBool(ctx, b) -proc toJS*[U, V](ctx: JSContext, t: Table[U, V]): JSValue = +proc toJS*[U, V](ctx: JSContext; t: Table[U, V]): JSValue = let obj = JS_NewObject(ctx) if not JS_IsException(obj): for k, v in t: definePropertyCWE(ctx, obj, k, v) return obj -proc toJS*(ctx: JSContext, opt: Option): JSValue = +proc toJS*(ctx: JSContext; opt: Option): JSValue = if opt.isSome: return toJS(ctx, opt.get) return JS_NULL -proc toJS[T, E](ctx: JSContext, opt: Result[T, E]): JSValue = +proc toJS[T, E](ctx: JSContext; opt: Result[T, E]): JSValue = if opt.isSome: when not (T is void): return toJS(ctx, opt.get) @@ -213,7 +213,7 @@ proc toJS[T, E](ctx: JSContext, opt: Result[T, E]): JSValue = return JS_Throw(ctx, res) return JS_EXCEPTION -proc toJS(ctx: JSContext, s: seq): JSValue = +proc toJS(ctx: JSContext; s: seq): JSValue = let a = JS_NewArray(ctx) if not JS_IsException(a): for i in 0..s.high: @@ -225,7 +225,7 @@ proc toJS(ctx: JSContext, s: seq): JSValue = return JS_EXCEPTION return a -proc toJS*[T](ctx: JSContext, s: set[T]): JSValue = +proc toJS*[T](ctx: JSContext; s: set[T]): JSValue = #TODO this is a bit lazy :p var x = newSeq[T]() for e in s: @@ -237,7 +237,7 @@ proc toJS*[T](ctx: JSContext, s: set[T]): JSValue = JS_FreeValue(ctx, a) return ret -proc toJS(ctx: JSContext, t: tuple): JSValue = +proc toJS(ctx: JSContext; t: tuple): JSValue = let a = JS_NewArray(ctx) if not JS_IsException(a): var i = 0 @@ -251,7 +251,7 @@ proc toJS(ctx: JSContext, t: tuple): JSValue = inc i return a -proc toJSP0(ctx: JSContext, p, tp: pointer, ctor: JSValue, +proc toJSP0(ctx: JSContext; p, tp: pointer; ctor: JSValue; needsref: var bool): JSValue = JS_GetRuntime(ctx).getOpaque().plist.withValue(p, obj): # a JSValue already points to this object. @@ -271,7 +271,7 @@ proc toJSP0(ctx: JSContext, p, tp: pointer, ctor: JSValue, JS_SetIsHTMLDDA(ctx, jsObj) return jsObj -proc toJSRefObj(ctx: JSContext, obj: ref object): JSValue = +proc toJSRefObj(ctx: JSContext; obj: ref object): JSValue = if obj == nil: return JS_NULL let p = cast[pointer](obj) @@ -282,10 +282,10 @@ proc toJSRefObj(ctx: JSContext, obj: ref object): JSValue = GC_ref(obj) return val -proc toJS*(ctx: JSContext, obj: ref object): JSValue = +proc toJS*(ctx: JSContext; obj: ref object): JSValue = return toJSRefObj(ctx, obj) -proc toJSNew*(ctx: JSContext, obj: ref object, ctor: JSValue): JSValue = +proc toJSNew*(ctx: JSContext; obj: ref object; ctor: JSValue): JSValue = if obj == nil: return JS_NULL let p = cast[pointer](obj) @@ -296,7 +296,7 @@ proc toJSNew*(ctx: JSContext, obj: ref object, ctor: JSValue): JSValue = GC_ref(obj) return val -proc toJSNew[T, E](ctx: JSContext, opt: Result[T, E], ctor: JSValue): JSValue = +proc toJSNew[T, E](ctx: JSContext; opt: Result[T, E], ctor: JSValue): JSValue = if opt.isSome: when not (T is void): return toJSNew(ctx, opt.get, ctor) @@ -310,13 +310,13 @@ proc toJSNew[T, E](ctx: JSContext, opt: Result[T, E], ctor: JSValue): JSValue = else: return JS_NULL -proc toJS(ctx: JSContext, e: enum): JSValue = +proc toJS(ctx: JSContext; e: enum): JSValue = return toJS(ctx, $e) -proc toJS(ctx: JSContext, j: JSValue): JSValue = +proc toJS(ctx: JSContext; j: JSValue): JSValue = return j -proc toJS(ctx: JSContext, promise: EmptyPromise): JSValue = +proc toJS(ctx: JSContext; promise: EmptyPromise): JSValue = var resolving_funcs: array[2, JSValue] let jsPromise = JS_NewPromiseCapability(ctx, addr resolving_funcs[0]) if JS_IsException(jsPromise): @@ -329,7 +329,7 @@ proc toJS(ctx: JSContext, promise: EmptyPromise): JSValue = JS_FreeValue(ctx, resolving_funcs[1])) return jsPromise -proc toJS[T](ctx: JSContext, promise: Promise[T]): JSValue = +proc toJS[T](ctx: JSContext; promise: Promise[T]): JSValue = var resolving_funcs: array[2, JSValue] let jsPromise = JS_NewPromiseCapability(ctx, addr resolving_funcs[0]) if JS_IsException(jsPromise): @@ -343,7 +343,7 @@ proc toJS[T](ctx: JSContext, promise: Promise[T]): JSValue = JS_FreeValue(ctx, resolving_funcs[1])) return jsPromise -proc toJS[T, E](ctx: JSContext, promise: Promise[Result[T, E]]): JSValue = +proc toJS[T, E](ctx: JSContext; promise: Promise[Result[T, E]]): JSValue = var resolving_funcs: array[2, JSValue] let jsPromise = JS_NewPromiseCapability(ctx, addr resolving_funcs[0]) if JS_IsException(jsPromise): @@ -369,7 +369,7 @@ proc toJS[T, E](ctx: JSContext, promise: Promise[Result[T, E]]): JSValue = JS_FreeValue(ctx, resolving_funcs[1])) return jsPromise -proc toJS*(ctx: JSContext, err: JSError): JSValue = +proc toJS*(ctx: JSContext; err: JSError): JSValue = if err.e notin QuickJSErrors: return toJSRefObj(ctx, err) var msg = toJS(ctx, err.message) @@ -380,20 +380,20 @@ proc toJS*(ctx: JSContext, err: JSError): JSValue = JS_FreeValue(ctx, msg) return ret -proc toJS*(ctx: JSContext, abuf: JSArrayBuffer): JSValue = +proc toJS*(ctx: JSContext; abuf: JSArrayBuffer): JSValue = return JS_NewArrayBuffer(ctx, abuf.p, abuf.len, abuf.dealloc, nil, false) -proc toJS*(ctx: JSContext, u8a: JSUint8Array): JSValue = +proc toJS*(ctx: JSContext; u8a: JSUint8Array): JSValue = var jsabuf = toJS(ctx, u8a.abuf) let ctor = ctx.getOpaque().Uint8Array_ctor let ret = JS_CallConstructor(ctx, ctor, 1, addr jsabuf) JS_FreeValue(ctx, jsabuf) return ret -proc toJS*(ctx: JSContext, ns: NarrowString): JSValue = +proc toJS*(ctx: JSContext; ns: NarrowString): JSValue = return JS_NewNarrowStringLen(ctx, cstring(ns), csize_t(string(ns).len)) -proc toJS*(ctx: JSContext, dict: JSDict): JSValue = +proc toJS*(ctx: JSContext; dict: JSDict): JSValue = let obj = JS_NewObject(ctx) if JS_IsException(obj): return obj @@ -401,7 +401,7 @@ proc toJS*(ctx: JSContext, dict: JSDict): JSValue = ctx.defineProperty(obj, k, v) return obj -proc toJSP(ctx: JSContext, parent: ref object, child: var object): JSValue = +proc toJSP(ctx: JSContext; parent: ref object; child: var object): JSValue = let p = addr child # Save parent as the original ancestor for this tree. JS_GetRuntime(ctx).getOpaque().refmap[p] = ( @@ -417,7 +417,7 @@ proc toJSP(ctx: JSContext, parent: ref object, child: var object): JSValue = GC_ref(parent) return val -proc toJSP(ctx: JSContext, parent: ptr object, child: var object): JSValue = +proc toJSP(ctx: JSContext; parent: ptr object; child: var object): JSValue = let p = addr child # Increment the reference count of parent's root ancestor, and save the # increment/decrement callbacks for the child as well. |