diff --git a/lib/js/jsarrays.nim b/lib/js/jsarrays.nim new file mode 100644 index 0000000000000..22d75620532a0 --- /dev/null +++ b/lib/js/jsarrays.nim @@ -0,0 +1,78 @@ +##[ +This module provides a wrapper for ArrayBuffer, DataView and related API's such +as `getInt8`, `setInt16`. +]## + +#[ +`BigInt` could be homed here. +]# + +static: doAssert defined(js) + +type + ArrayBuffer* = ref object {.importjs.} + DataView* = ref object {.importjs.} + ## https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView + +proc newArrayBuffer*(n: int): ArrayBuffer {.importjs: "new ArrayBuffer(#)".} +proc newDataView*(a: ArrayBuffer, offset: int): DataView {.importjs: "new DataView(#, #)".} +proc toArrayBuffer*(a: string): ArrayBuffer = + let n = a.len + result = newArrayBuffer(n) + {.emit:""" + const view = new Uint8Array(`result`); + for(i=0;i<`n`;i++){ + view[i] = `a`[i]; + } + """.} + +template genGetSet(T, funGet, funSet): untyped = + proc funGet(a: DataView, byteOffset: int, littleEndian: bool): T {.importcpp.} + proc funSet(a: DataView, byteOffset: int, value: T, littleEndian: bool): T {.importcpp.} + +genGetSet int8, getInt8, setInt8 +genGetSet int16, getInt16, setInt16 +genGetSet int32, getInt32, setInt32 + +proc getTyped*(a: DataView, T: typedesc, offset: int, littleEndian: bool): T = + when false: discard + elif T is int8: getInt8(a, offset, littleEndian) + elif T is int16: getInt16(a, offset, littleEndian) + elif T is int32: getInt32(a, offset, littleEndian) + else: static doAssert false, $T # add as needed + +when false: ## scratch below + # view[i] = str.charCodeAt(i); // check whether would be needed for cstring + + # proc `[]=`(a: ArrayBuffer, index: int, val: char) = + # {.emit: """`a`[`index`] = `val`;""".} + + # DataView.prototype.getBigInt64() + # DataView.prototype.getBigUint64() + # DataView.prototype.getFloat32() + # DataView.prototype.getFloat64() + # DataView.prototype.getInt16() + # DataView.prototype.getInt32() + # DataView.prototype.getInt8() + # DataView.prototype.getUint16() + # DataView.prototype.getUint32() + # DataView.prototype.getUint8() + # DataView.prototype.setBigInt64() + # DataView.prototype.setBigUint64() + # DataView.prototype.setFloat32() + # DataView.prototype.setFloat64() + # DataView.prototype.setInt16() + # DataView.prototype.setInt32() + # DataView.prototype.setInt8() + # DataView.prototype.setUint16() + # DataView.prototype.setUint32() + # DataView.prototype.setUint8() + + proc decode() = + case bufLen + of int32.sizeof: + cast[ptr int](buffer)[] = s2.getInt32(0, true) + of int16.sizeof: + cast[ptr int16](buffer)[] = s2.getInt16(0, true) + else: + doAssert false diff --git a/lib/pure/streams.nim b/lib/pure/streams.nim index 8efe5b227bfa5..ba4bc25b20c67 100644 --- a/lib/pure/streams.nim +++ b/lib/pure/streams.nim @@ -96,6 +96,9 @@ include "system/inclrtl" +when defined(js): + import std/jsarrays + const taintMode = compileOption("taintmode") proc newEIO(msg: string): owned(ref IOError) = @@ -393,8 +396,6 @@ proc writeLine*(s: Stream, args: varargs[string, `$`]) = proc read*[T](s: Stream, result: var T) = ## Generic read procedure. Reads `result` from the stream `s`. - ## - ## **Note:** Not available for JS backend. Use `readStr <#readStr,Stream,int>`_ for now. runnableExamples: var strm = newStringStream("012") ## readInt @@ -412,8 +413,6 @@ proc read*[T](s: Stream, result: var T) = proc peek*[T](s: Stream, result: var T) = ## Generic peek procedure. Peeks `result` from the stream `s`. - ## - ## **Note:** Not available for JS backend. Use `peekStr <#peekStr,Stream,int>`_ for now. runnableExamples: var strm = newStringStream("012") ## peekInt @@ -1134,6 +1133,10 @@ type ## This is updated when called `writeLine` etc. pos: int + when defined js: + data2: ArrayBuffer + dataView: DataView + when (NimMajor, NimMinor) < (1, 3) and defined(js): proc ssAtEnd(s: Stream): bool {.compileTime.} = var s = StringStream(s) @@ -1217,11 +1220,19 @@ else: # after 1.3 or JS not defined result = min(bufLen, s.data.len - s.pos) if result > 0: when defined(js): - try: - cast[ptr string](buffer)[][0..