diff options
| author | bptato <nincsnevem662@gmail.com> | 2024-05-08 18:39:27 +0200 |
|---|---|---|
| committer | bptato <nincsnevem662@gmail.com> | 2024-05-08 18:39:27 +0200 |
| commit | 86f4c56507bc94d03e7d94be6ede884b9e7b5358 (patch) | |
| tree | 929ce32ada67e8b1d57787d8e6d038fa603fc31e /src/js | |
| parent | 5c891488423aa7ccbef2fb4332ff6cb4e3c05154 (diff) | |
| download | chawan-86f4c56507bc94d03e7d94be6ede884b9e7b5358.tar.gz | |
js: refactor
* prefix to-be-separated modules with js
* remove dynstreams dependency
* untangle from EmptyPromise
* move typeptr into tojs
Diffstat (limited to 'src/js')
| -rw-r--r-- | src/js/console.nim | 9 | ||||
| -rw-r--r-- | src/js/domexception.nim | 2 | ||||
| -rw-r--r-- | src/js/encoding.nim | 2 | ||||
| -rw-r--r-- | src/js/fromjs.nim | 39 | ||||
| -rw-r--r-- | src/js/javascript.nim | 21 | ||||
| -rw-r--r-- | src/js/jscolor.nim | 2 | ||||
| -rw-r--r-- | src/js/jserror.nim (renamed from src/js/error.nim) | 0 | ||||
| -rw-r--r-- | src/js/jsmodule.nim (renamed from src/js/module.nim) | 0 | ||||
| -rw-r--r-- | src/js/jsopaque.nim (renamed from src/js/opaque.nim) | 2 | ||||
| -rw-r--r-- | src/js/jspropenumlist.nim (renamed from src/js/propertyenumlist.nim) | 0 | ||||
| -rw-r--r-- | src/js/jsregex.nim (renamed from src/js/regex.nim) | 0 | ||||
| -rw-r--r-- | src/js/tojs.nim | 85 | ||||
| -rw-r--r-- | src/js/typeptr.nim | 18 |
13 files changed, 43 insertions, 137 deletions
diff --git a/src/js/console.nim b/src/js/console.nim index 3b28df5e..712f228c 100644 --- a/src/js/console.nim +++ b/src/js/console.nim @@ -1,4 +1,5 @@ import io/dynstream +import js/jserror import js/javascript type Console* = ref object @@ -55,3 +56,11 @@ proc addConsoleModule*(ctx: JSContext) = #TODO console should not have a prototype # "For historical reasons, console is lowercased." ctx.registerType(Console, nointerface = true, name = "console") + +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() diff --git a/src/js/domexception.nim b/src/js/domexception.nim index 39537b0a..902cd618 100644 --- a/src/js/domexception.nim +++ b/src/js/domexception.nim @@ -1,7 +1,7 @@ import std/tables import bindings/quickjs -import js/error +import js/jserror import js/javascript const NamesTable = { diff --git a/src/js/encoding.nim b/src/js/encoding.nim index cfcf8f07..edf0cf97 100644 --- a/src/js/encoding.nim +++ b/src/js/encoding.nim @@ -1,5 +1,5 @@ import bindings/quickjs -import js/error +import js/jserror import js/javascript import js/jstypes diff --git a/src/js/fromjs.nim b/src/js/fromjs.nim index c5c80da1..7a8a0859 100644 --- a/src/js/fromjs.nim +++ b/src/js/fromjs.nim @@ -4,11 +4,9 @@ import std/tables import std/unicode import bindings/quickjs -import io/promise -import js/error +import js/jserror import js/jstypes -import js/jsutils -import js/opaque +import js/jsopaque import types/opt import utils/twtstr @@ -374,37 +372,6 @@ proc fromJSArrayBufferView(ctx: JSContext; val: JSValue): ) return ok(view) -proc promiseThenCallback(ctx: JSContext; this_val: JSValue; argc: cint; - argv: ptr UncheckedArray[JSValue]; magic: cint; - func_data: ptr UncheckedArray[JSValue]): JSValue {.cdecl.} = - let fun = func_data[0] - let op = JS_GetOpaque(fun, JS_GetClassID(fun)) - if op != nil: - let p = cast[EmptyPromise](op) - p.resolve() - GC_unref(p) - JS_SetOpaque(fun, nil) - return JS_UNDEFINED - -proc fromJSEmptyPromise(ctx: JSContext; val: JSValue): JSResult[EmptyPromise] = - if not JS_IsObject(val): - return err(newTypeError("Value is not an object")) - var p = EmptyPromise() - GC_ref(p) - let tmp = JS_NewObject(ctx) - JS_SetOpaque(tmp, cast[pointer](p)) - let fun = JS_NewCFunctionData(ctx, promiseThenCallback, 0, 0, 1, - tmp.toJSValueArray()) - JS_FreeValue(ctx, tmp) - let res = JS_Invoke(ctx, val, ctx.getOpaque().strRefs[jstThen], 1, - fun.toJSValueArray()) - JS_FreeValue(ctx, fun) - if JS_IsException(res): - JS_FreeValue(ctx, res) - return err() - JS_FreeValue(ctx, res) - return ok(p) - type FromJSAllowedT = (object and not (Result|Option|Table|JSValue|JSDict| JSArrayBuffer|JSArrayBufferView|JSUint8Array)) @@ -437,8 +404,6 @@ proc fromJS*[T](ctx: JSContext; val: JSValue): JSResult[T] = return fromJSEnum[T](ctx, val) elif T is JSValue: return ok(val) - elif T is EmptyPromise: - return fromJSEmptyPromise(ctx, val) elif T is ref object: return fromJSObject[T](ctx, val) elif T is void: diff --git a/src/js/javascript.nim b/src/js/javascript.nim index 90694e93..58b80c6f 100644 --- a/src/js/javascript.nim +++ b/src/js/javascript.nim @@ -48,12 +48,10 @@ import std/strutils import std/tables import std/unicode -import io/dynstream -import js/error +import js/jserror import js/fromjs -import js/opaque +import js/jsopaque import js/tojs -import js/typeptr import types/opt import utils/twtstr @@ -225,20 +223,15 @@ proc getExceptionMsg*(ctx: JSContext; err: JSError): string = 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) = +# Returns early with err(JSContext) if an exception was thrown in a +# context. +proc runJSJobs*(rt: JSRuntime): Err[JSContext] = while JS_IsJobPending(rt): var ctx: JSContext let r = JS_ExecutePendingJob(rt, addr ctx) if r == -1: - ctx.writeException(err) + return err(ctx) + ok() # Add all LegacyUnforgeable functions defined on the prototype chain to # the opaque. diff --git a/src/js/jscolor.nim b/src/js/jscolor.nim index 48f7fb34..a73fb599 100644 --- a/src/js/jscolor.nim +++ b/src/js/jscolor.nim @@ -1,7 +1,7 @@ import std/strutils import bindings/quickjs -import js/error +import js/jserror import js/fromjs import js/javascript import js/tojs diff --git a/src/js/error.nim b/src/js/jserror.nim index b4101830..b4101830 100644 --- a/src/js/error.nim +++ b/src/js/jserror.nim diff --git a/src/js/module.nim b/src/js/jsmodule.nim index 636065b8..636065b8 100644 --- a/src/js/module.nim +++ b/src/js/jsmodule.nim diff --git a/src/js/opaque.nim b/src/js/jsopaque.nim index 93900502..bfe8b07b 100644 --- a/src/js/opaque.nim +++ b/src/js/jsopaque.nim @@ -1,7 +1,7 @@ import std/tables import bindings/quickjs -import js/error +import js/jserror import types/opt type diff --git a/src/js/propertyenumlist.nim b/src/js/jspropenumlist.nim index 65c16c2f..65c16c2f 100644 --- a/src/js/propertyenumlist.nim +++ b/src/js/jspropenumlist.nim diff --git a/src/js/regex.nim b/src/js/jsregex.nim index f24633d1..f24633d1 100644 --- a/src/js/regex.nim +++ b/src/js/jsregex.nim diff --git a/src/js/tojs.nim b/src/js/tojs.nim index 4090757d..7697ea4a 100644 --- a/src/js/tojs.nim +++ b/src/js/tojs.nim @@ -4,8 +4,6 @@ # # * Primitives are converted to their respective JavaScript counterparts. # * seq is converted to a JS array. Note: this always copies the seq's contents. -# * Promise is converted to a JS promise which will be resolved when the Nim -# promise is resolved. # * enum is converted to its stringifier's output. # * JSValue is returned as-is, *without* a DupValue operation. # * JSError is converted to a new error object corresponding to the error @@ -41,12 +39,10 @@ import std/tables import std/unicode import bindings/quickjs -import io/promise -import js/error +import js/jserror import js/jstypes import js/jsutils -import js/opaque -import js/typeptr +import js/jsopaque import types/opt # Convert Nim types to the corresponding JavaScript type. @@ -69,9 +65,6 @@ 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 @@ -273,6 +266,25 @@ proc toJSP0(ctx: JSContext; p, tp: pointer; ctor: JSValue; JS_SetIsHTMLDDA(ctx, jsObj) return jsObj +# Get a unique pointer for each type. +proc getTypePtr*[T](x: T): pointer = + when T is RootRef: + # I'm so sorry. + # (This dereferences the object's first member, m_type. Probably.) + return cast[ptr pointer](x)[] + elif T is RootObj: + return cast[pointer](x) + else: + return getTypeInfo(x) + +func getTypePtr*(t: typedesc[ref object]): pointer = + var x = t() + return getTypePtr(x) + +func getTypePtr*(t: type): pointer = + var x: t + return getTypePtr(x) + proc toJSRefObj(ctx: JSContext; obj: ref object): JSValue = if obj == nil: return JS_NULL @@ -318,61 +330,6 @@ proc toJS(ctx: JSContext; e: enum): JSValue = proc toJS(ctx: JSContext; j: JSValue): JSValue = return j -proc toJS(ctx: JSContext; promise: EmptyPromise): JSValue = - var resolving_funcs: array[2, JSValue] - let jsPromise = JS_NewPromiseCapability(ctx, resolving_funcs.toJSValueArray()) - if JS_IsException(jsPromise): - return JS_EXCEPTION - promise.then(proc() = - let res = JS_Call(ctx, resolving_funcs[0], JS_UNDEFINED, 0, nil) - JS_FreeValue(ctx, res) - JS_FreeValue(ctx, resolving_funcs[0]) - JS_FreeValue(ctx, resolving_funcs[1])) - return jsPromise - -proc toJS[T](ctx: JSContext; promise: Promise[T]): JSValue = - var resolving_funcs: array[2, JSValue] - let jsPromise = JS_NewPromiseCapability(ctx, resolving_funcs.toJSValueArray()) - if JS_IsException(jsPromise): - return JS_EXCEPTION - promise.then(proc(x: T) = - let x = toJS(ctx, x) - let res = JS_Call(ctx, resolving_funcs[0], JS_UNDEFINED, 1, - x.toJSValueArray()) - JS_FreeValue(ctx, res) - JS_FreeValue(ctx, x) - JS_FreeValue(ctx, resolving_funcs[0]) - JS_FreeValue(ctx, resolving_funcs[1])) - return jsPromise - -proc toJS[T, E](ctx: JSContext; promise: Promise[Result[T, E]]): JSValue = - var resolving_funcs: array[2, JSValue] - let jsPromise = JS_NewPromiseCapability(ctx, resolving_funcs.toJSValueArray()) - if JS_IsException(jsPromise): - return JS_EXCEPTION - promise.then(proc(x: Result[T, E]) = - if x.isSome: - let x = when T is void: - JS_UNDEFINED - else: - toJS(ctx, x.get) - let res = JS_Call(ctx, resolving_funcs[0], JS_UNDEFINED, 1, - x.toJSValueArray()) - JS_FreeValue(ctx, res) - JS_FreeValue(ctx, x) - else: # err - let x = when E is void: - JS_UNDEFINED - else: - toJS(ctx, x.error) - let res = JS_Call(ctx, resolving_funcs[1], JS_UNDEFINED, 1, - x.toJSValueArray()) - JS_FreeValue(ctx, res) - JS_FreeValue(ctx, x) - JS_FreeValue(ctx, resolving_funcs[0]) - JS_FreeValue(ctx, resolving_funcs[1])) - return jsPromise - proc toJS*(ctx: JSContext; err: JSError): JSValue = if err == nil: return JS_EXCEPTION diff --git a/src/js/typeptr.nim b/src/js/typeptr.nim deleted file mode 100644 index 55fd89c7..00000000 --- a/src/js/typeptr.nim +++ /dev/null @@ -1,18 +0,0 @@ -# Get a unique pointer for each type. -proc getTypePtr*[T](x: T): pointer = - when T is RootRef: - # I'm so sorry. - # (This dereferences the object's first member, m_type. Probably.) - return cast[ptr pointer](x)[] - elif T is RootObj: - return cast[pointer](x) - else: - return getTypeInfo(x) - -func getTypePtr*(t: typedesc[ref object]): pointer = - var x = t() - return getTypePtr(x) - -func getTypePtr*(t: type): pointer = - var x: t - return getTypePtr(x) |