diff options
author | bptato <nincsnevem662@gmail.com> | 2024-01-11 18:25:46 +0100 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2024-01-11 18:25:46 +0100 |
commit | aeb9b9e5dddf3102773aa900865fe11a91cd65a6 (patch) | |
tree | ebd82f907877ea9caf3826f5ce993772e714d36c /src/js | |
parent | 14c9c373a89ddd2c64d4178877db168253ff776d (diff) | |
download | chawan-aeb9b9e5dddf3102773aa900865fe11a91cd65a6.tar.gz |
js: merge some type modules into jstypes
They only had type definitions, no need to put them in separate modules.
Diffstat (limited to 'src/js')
-rw-r--r-- | src/js/arraybuffer.nim | 18 | ||||
-rw-r--r-- | src/js/base64.nim | 2 | ||||
-rw-r--r-- | src/js/dict.nim | 5 | ||||
-rw-r--r-- | src/js/encoding.nim | 3 | ||||
-rw-r--r-- | src/js/fromjs.nim | 12 | ||||
-rw-r--r-- | src/js/intl.nim | 2 | ||||
-rw-r--r-- | src/js/jstypes.nim | 32 | ||||
-rw-r--r-- | src/js/strings.nim | 3 | ||||
-rw-r--r-- | src/js/tojs.nim | 12 |
9 files changed, 45 insertions, 44 deletions
diff --git a/src/js/arraybuffer.nim b/src/js/arraybuffer.nim deleted file mode 100644 index ff9fa24d..00000000 --- a/src/js/arraybuffer.nim +++ /dev/null @@ -1,18 +0,0 @@ -import bindings/quickjs - -type - JSArrayBuffer* = object - p*: ptr UncheckedArray[uint8] - len*: csize_t - dealloc*: JSFreeArrayBufferDataFunc - - JSArrayBufferView* = object - abuf*: JSArrayBuffer - offset*: csize_t # offset into the buffer - nmemb*: csize_t # number of members - nsize*: csize_t # member size - - JSUint8Array* = object - abuf*: JSArrayBuffer - offset*: csize_t # offset into the buffer - nmemb*: csize_t # number of members diff --git a/src/js/base64.nim b/src/js/base64.nim index 912843b4..f72131c3 100644 --- a/src/js/base64.nim +++ b/src/js/base64.nim @@ -3,7 +3,7 @@ import std/base64 import bindings/quickjs import js/domexception import js/javascript -import js/strings +import js/jstypes import types/opt # atob and btoa convert Latin-1 to base64 and vice versa. (And throw on diff --git a/src/js/dict.nim b/src/js/dict.nim deleted file mode 100644 index 5003b92c..00000000 --- a/src/js/dict.nim +++ /dev/null @@ -1,5 +0,0 @@ -# This is the WebIDL dictionary type. -# We only use it for type inference in generics. -#TODO required members - -type JSDict* = object of RootObj diff --git a/src/js/encoding.nim b/src/js/encoding.nim index 31f6f575..0e6643fb 100644 --- a/src/js/encoding.nim +++ b/src/js/encoding.nim @@ -1,9 +1,8 @@ import std/streams -import js/arraybuffer -import js/dict import js/error import js/javascript +import js/jstypes import chakasu/charset import chakasu/decoderstream diff --git a/src/js/fromjs.nim b/src/js/fromjs.nim index cf3abc6d..d7f34905 100644 --- a/src/js/fromjs.nim +++ b/src/js/fromjs.nim @@ -6,9 +6,8 @@ import std/unicode import bindings/quickjs import io/promise -import js/arraybuffer -import js/dict import js/error +import js/jstypes import js/opaque import js/tojs import types/opt @@ -115,12 +114,11 @@ func fromJSInt[T: SomeInteger](ctx: JSContext, val: JSValue): return err() return ok(cast[uint64](ret)) -proc fromJSFloat[T: SomeFloat](ctx: JSContext, val: JSValue): - JSResult[T] = +proc fromJSFloat64(ctx: JSContext, val: JSValue): JSResult[float64] = var f64: float64 if JS_ToFloat64(ctx, addr f64, val) < 0: return err() - return ok(cast[T](f64)) + return ok(f64) macro fromJSTupleBody(a: tuple) = let len = a.getType().len - 1 @@ -499,8 +497,8 @@ proc fromJS*[T](ctx: JSContext, val: JSValue): JSResult[T] = typeof(result.get.values)](ctx, val) elif T is SomeInteger: return fromJSInt[T](ctx, val) - elif T is SomeFloat: - return fromJSFloat[T](ctx, val) + elif T is float64: + return fromJSFloat64(ctx, val) elif T is enum: return fromJSEnum[T](ctx, val) elif T is JSValue: diff --git a/src/js/intl.nim b/src/js/intl.nim index 285a2b4e..9e17edf3 100644 --- a/src/js/intl.nim +++ b/src/js/intl.nim @@ -1,8 +1,8 @@ # Very minimal Intl module... TODO make it more complete import bindings/quickjs -import js/dict import js/javascript +import js/jstypes import js/tojs type diff --git a/src/js/jstypes.nim b/src/js/jstypes.nim new file mode 100644 index 00000000..b8ef6c55 --- /dev/null +++ b/src/js/jstypes.nim @@ -0,0 +1,32 @@ +import bindings/quickjs + +# This is the WebIDL dictionary type. +# We only use it for type inference in generics. +#TODO required members +type JSDict* = object of RootObj + +# Containers compatible with the internal representation of strings in QuickJS. +# To convert these, a copy is still needed; however, they remove the UTF-8 +# transcoding step. +type + NarrowString* = distinct string + WideString* = distinct seq[uint16] + +# Various containers for array buffer types. +# Converting these only requires copying the metadata; buffers are never copied. +type + JSArrayBuffer* = object + p*: ptr UncheckedArray[uint8] + len*: csize_t + dealloc*: JSFreeArrayBufferDataFunc + + JSArrayBufferView* = object + abuf*: JSArrayBuffer + offset*: csize_t # offset into the buffer + nmemb*: csize_t # number of members + nsize*: csize_t # member size + + JSUint8Array* = object + abuf*: JSArrayBuffer + offset*: csize_t # offset into the buffer + nmemb*: csize_t # number of members diff --git a/src/js/strings.nim b/src/js/strings.nim deleted file mode 100644 index ab0dd753..00000000 --- a/src/js/strings.nim +++ /dev/null @@ -1,3 +0,0 @@ -type - NarrowString* = distinct string - WideString* = distinct seq[uint16] diff --git a/src/js/tojs.nim b/src/js/tojs.nim index 91b1128e..2936ba28 100644 --- a/src/js/tojs.nim +++ b/src/js/tojs.nim @@ -13,7 +13,7 @@ # * JSArrayBuffer, JSUint8Array are converted to a JS object without copying # their contents. # * NarrowString is converted to a JS narrow string (with copying). For more -# information on JS string handling, see js/strings.nim. +# information on JS string handling, see js/jstypes.nim. # * Finally, ref object is converted to a JS object whose opaque is the ref # object. (See below.) # @@ -42,11 +42,9 @@ import std/unicode import bindings/quickjs import io/promise -import js/arraybuffer -import js/dict import js/error +import js/jstypes import js/opaque -import js/strings import js/typeptr import types/opt @@ -60,7 +58,7 @@ 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: SomeFloat): 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 @@ -158,8 +156,8 @@ proc toJS*(ctx: JSContext, n: uint64): JSValue = #TODO this is incorrect return JS_NewFloat64(ctx, float64(n)) -proc toJS*(ctx: JSContext, n: SomeFloat): JSValue = - return JS_NewFloat64(ctx, float64(n)) +proc toJS*(ctx: JSContext, n: float64): JSValue = + return JS_NewFloat64(ctx, n) proc toJS*(ctx: JSContext, b: bool): JSValue = return JS_NewBool(ctx, b) |