quickjs-emscripten
Advanced tools
Comparing version 0.23.0 to 0.24.0
@@ -181,2 +181,6 @@ import { QuickJSDeferredPromise } from "./deferred-promise"; | ||
/** | ||
* Create a new QuickJS [ArrayBuffer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer). | ||
*/ | ||
newArrayBuffer(buffer: ArrayBufferLike): QuickJSHandle; | ||
/** | ||
* Create a new [[QuickJSDeferredPromise]]. Use `deferred.resolve(handle)` and | ||
@@ -254,2 +258,6 @@ * `deferred.reject(handle)` to fulfill the promise handle available at `deferred.handle`. | ||
/** | ||
* Coverts `handle` to a JavaScript ArrayBuffer | ||
*/ | ||
getArrayBuffer(handle: QuickJSHandle): Lifetime<Uint8Array>; | ||
/** | ||
* `Promise.resolve(value)`. | ||
@@ -372,3 +380,30 @@ * Convert a handle containing a Promise-like value inside the VM into an | ||
private errorToHandle; | ||
/** | ||
* Outputs QuickJS Objects in binary form | ||
* | ||
* **WARNING**: QuickJS's binary JSON doesn't have a standard so expect it to change between version | ||
* | ||
* ```ts | ||
* // imagine sending data to another via IPC | ||
* let dataLifetime = context.newString("This is an example") | ||
* ?.consume(handle => context.encodeBinaryJSON(handle)) | ||
* ?.consume(handle => context.getArrayBuffer(handle)) | ||
* socket.write(dataLifetime?.value) | ||
* ``` | ||
*/ | ||
encodeBinaryJSON(handle: QuickJSHandle): QuickJSHandle; | ||
/** | ||
* Outputs Handle of the given QuickJS Object in binary form | ||
* | ||
* ```ts | ||
* // imagine receiving data from another via IPC | ||
* socket.on("data", chunk => { | ||
* context.newArrayBuffer(chunk) | ||
* ?.consume(handle => context.decodeBinaryJSON(handle)) | ||
* ?.consume(handle => console.log(context.dump(handle))) | ||
* }) | ||
* ``` | ||
*/ | ||
decodeBinaryJSON(handle: QuickJSHandle): QuickJSHandle; | ||
} | ||
export {}; |
@@ -311,2 +311,11 @@ "use strict"; | ||
} | ||
/** | ||
* Create a new QuickJS [ArrayBuffer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer). | ||
*/ | ||
newArrayBuffer(buffer) { | ||
const array = new Uint8Array(buffer); | ||
const handle = this.memory.newHeapBufferPointer(array); | ||
const ptr = this.ffi.QTS_NewArrayBuffer(this.ctx.value, handle.value.pointer, array.length); | ||
return this.memory.heapValueHandle(ptr); | ||
} | ||
newPromise(value) { | ||
@@ -422,2 +431,14 @@ const deferredPromise = lifetime_1.Scope.withScope((scope) => { | ||
/** | ||
* Coverts `handle` to a JavaScript ArrayBuffer | ||
*/ | ||
getArrayBuffer(handle) { | ||
this.runtime.assertOwned(handle); | ||
const len = this.ffi.QTS_GetArrayBufferLength(this.ctx.value, handle.value); | ||
const ptr = this.ffi.QTS_GetArrayBuffer(this.ctx.value, handle.value); | ||
if (!ptr) { | ||
throw new Error("Couldn't allocate memory to get ArrayBuffer"); | ||
} | ||
return new lifetime_1.Lifetime(this.module.HEAPU8.subarray(ptr, ptr + len), undefined, (value) => this.module._free(ptr)); | ||
} | ||
/** | ||
* `Promise.resolve(value)`. | ||
@@ -691,4 +712,37 @@ * Convert a handle containing a Promise-like value inside the VM into an | ||
} | ||
/** | ||
* Outputs QuickJS Objects in binary form | ||
* | ||
* **WARNING**: QuickJS's binary JSON doesn't have a standard so expect it to change between version | ||
* | ||
* ```ts | ||
* // imagine sending data to another via IPC | ||
* let dataLifetime = context.newString("This is an example") | ||
* ?.consume(handle => context.encodeBinaryJSON(handle)) | ||
* ?.consume(handle => context.getArrayBuffer(handle)) | ||
* socket.write(dataLifetime?.value) | ||
* ``` | ||
*/ | ||
encodeBinaryJSON(handle) { | ||
const ptr = this.ffi.QTS_bjson_encode(this.ctx.value, handle.value); | ||
return this.memory.heapValueHandle(ptr); | ||
} | ||
/** | ||
* Outputs Handle of the given QuickJS Object in binary form | ||
* | ||
* ```ts | ||
* // imagine receiving data from another via IPC | ||
* socket.on("data", chunk => { | ||
* context.newArrayBuffer(chunk) | ||
* ?.consume(handle => context.decodeBinaryJSON(handle)) | ||
* ?.consume(handle => console.log(context.dump(handle))) | ||
* }) | ||
* ``` | ||
*/ | ||
decodeBinaryJSON(handle) { | ||
const ptr = this.ffi.QTS_bjson_decode(this.ctx.value, handle.value); | ||
return this.memory.heapValueHandle(ptr); | ||
} | ||
} | ||
exports.QuickJSContext = QuickJSContext; | ||
//# sourceMappingURL=context.js.map |
export = QuickJSRaw; | ||
declare function QuickJSRaw(QuickJSRaw?: {}, ...args: any[]): any; | ||
declare namespace QuickJSRaw { | ||
export { QuickJSRaw }; | ||
} | ||
declare function QuickJSRaw(moduleArg?: {}, ...args: any[]): any; |
export = QuickJSRaw; | ||
declare function QuickJSRaw(QuickJSRaw?: {}, ...args: any[]): any; | ||
declare namespace QuickJSRaw { | ||
export { QuickJSRaw }; | ||
} | ||
declare function QuickJSRaw(moduleArg?: {}, ...args: any[]): any; |
export = QuickJSRaw; | ||
declare function QuickJSRaw(QuickJSRaw?: {}): any; | ||
declare namespace QuickJSRaw { | ||
export { QuickJSRaw }; | ||
} | ||
declare function QuickJSRaw(moduleArg?: {}): any; |
export = QuickJSRaw; | ||
declare function QuickJSRaw(QuickJSRaw?: {}): any; | ||
declare namespace QuickJSRaw { | ||
export { QuickJSRaw }; | ||
} | ||
declare function QuickJSRaw(moduleArg?: {}): any; |
@@ -39,2 +39,3 @@ import { QuickJSAsyncEmscriptenModule } from "../emscripten-types"; | ||
QTS_NewArray: (ctx: JSContextPointer) => JSValuePointer; | ||
QTS_NewArrayBuffer: (ctx: JSContextPointer, buffer: JSVoidPointer, length: number) => JSValuePointer; | ||
QTS_NewFloat64: (ctx: JSContextPointer, num: number) => JSValuePointer; | ||
@@ -44,2 +45,4 @@ QTS_GetFloat64: (ctx: JSContextPointer, value: JSValuePointer | JSValueConstPointer) => number; | ||
QTS_GetString: (ctx: JSContextPointer, value: JSValuePointer | JSValueConstPointer) => JSBorrowedCharPointer; | ||
QTS_GetArrayBuffer: (ctx: JSContextPointer, data: JSValuePointer | JSValueConstPointer) => JSVoidPointer; | ||
QTS_GetArrayBufferLength: (ctx: JSContextPointer, data: JSValuePointer | JSValueConstPointer) => number; | ||
QTS_NewSymbol: (ctx: JSContextPointer, description: BorrowedHeapCharPointer, isGlobal: number) => JSValuePointer; | ||
@@ -76,2 +79,4 @@ QTS_GetSymbolDescriptionOrKey: (ctx: JSContextPointer, value: JSValuePointer | JSValueConstPointer) => JSBorrowedCharPointer; | ||
QTS_RuntimeDisableModuleLoader: (rt: JSRuntimePointer) => void; | ||
QTS_bjson_encode: (ctx: JSContextPointer, val: JSValuePointer | JSValueConstPointer) => JSValuePointer; | ||
QTS_bjson_decode: (ctx: JSContextPointer, data: JSValuePointer | JSValueConstPointer) => JSValuePointer; | ||
} |
@@ -41,2 +41,3 @@ "use strict"; | ||
this.QTS_NewArray = this.module.cwrap("QTS_NewArray", "number", ["number"]); | ||
this.QTS_NewArrayBuffer = this.module.cwrap("QTS_NewArrayBuffer", "number", ["number", "number", "number"]); | ||
this.QTS_NewFloat64 = this.module.cwrap("QTS_NewFloat64", "number", ["number", "number"]); | ||
@@ -46,2 +47,4 @@ this.QTS_GetFloat64 = this.module.cwrap("QTS_GetFloat64", "number", ["number", "number"]); | ||
this.QTS_GetString = this.module.cwrap("QTS_GetString", "number", ["number", "number"]); | ||
this.QTS_GetArrayBuffer = this.module.cwrap("QTS_GetArrayBuffer", "number", ["number", "number"]); | ||
this.QTS_GetArrayBufferLength = this.module.cwrap("QTS_GetArrayBufferLength", "number", ["number", "number"]); | ||
this.QTS_NewSymbol = this.module.cwrap("QTS_NewSymbol", "number", ["number", "number", "number"]); | ||
@@ -78,2 +81,4 @@ this.QTS_GetSymbolDescriptionOrKey = (0, types_ffi_1.assertSync)(this.module.cwrap("QTS_GetSymbolDescriptionOrKey", "number", ["number", "number"])); | ||
this.QTS_RuntimeDisableModuleLoader = this.module.cwrap("QTS_RuntimeDisableModuleLoader", null, ["number"]); | ||
this.QTS_bjson_encode = this.module.cwrap("QTS_bjson_encode", "number", ["number", "number"]); | ||
this.QTS_bjson_decode = this.module.cwrap("QTS_bjson_decode", "number", ["number", "number"]); | ||
} | ||
@@ -80,0 +85,0 @@ } |
@@ -39,2 +39,3 @@ import { QuickJSEmscriptenModule } from "../emscripten-types"; | ||
QTS_NewArray: (ctx: JSContextPointer) => JSValuePointer; | ||
QTS_NewArrayBuffer: (ctx: JSContextPointer, buffer: JSVoidPointer, length: number) => JSValuePointer; | ||
QTS_NewFloat64: (ctx: JSContextPointer, num: number) => JSValuePointer; | ||
@@ -44,2 +45,4 @@ QTS_GetFloat64: (ctx: JSContextPointer, value: JSValuePointer | JSValueConstPointer) => number; | ||
QTS_GetString: (ctx: JSContextPointer, value: JSValuePointer | JSValueConstPointer) => JSBorrowedCharPointer; | ||
QTS_GetArrayBuffer: (ctx: JSContextPointer, data: JSValuePointer | JSValueConstPointer) => JSVoidPointer; | ||
QTS_GetArrayBufferLength: (ctx: JSContextPointer, data: JSValuePointer | JSValueConstPointer) => number; | ||
QTS_NewSymbol: (ctx: JSContextPointer, description: BorrowedHeapCharPointer, isGlobal: number) => JSValuePointer; | ||
@@ -69,2 +72,4 @@ QTS_GetSymbolDescriptionOrKey: (ctx: JSContextPointer, value: JSValuePointer | JSValueConstPointer) => JSBorrowedCharPointer; | ||
QTS_RuntimeDisableModuleLoader: (rt: JSRuntimePointer) => void; | ||
QTS_bjson_encode: (ctx: JSContextPointer, val: JSValuePointer | JSValueConstPointer) => JSValuePointer; | ||
QTS_bjson_decode: (ctx: JSContextPointer, data: JSValuePointer | JSValueConstPointer) => JSValuePointer; | ||
} |
@@ -40,2 +40,3 @@ "use strict"; | ||
this.QTS_NewArray = this.module.cwrap("QTS_NewArray", "number", ["number"]); | ||
this.QTS_NewArrayBuffer = this.module.cwrap("QTS_NewArrayBuffer", "number", ["number", "number", "number"]); | ||
this.QTS_NewFloat64 = this.module.cwrap("QTS_NewFloat64", "number", ["number", "number"]); | ||
@@ -45,2 +46,4 @@ this.QTS_GetFloat64 = this.module.cwrap("QTS_GetFloat64", "number", ["number", "number"]); | ||
this.QTS_GetString = this.module.cwrap("QTS_GetString", "number", ["number", "number"]); | ||
this.QTS_GetArrayBuffer = this.module.cwrap("QTS_GetArrayBuffer", "number", ["number", "number"]); | ||
this.QTS_GetArrayBufferLength = this.module.cwrap("QTS_GetArrayBufferLength", "number", ["number", "number"]); | ||
this.QTS_NewSymbol = this.module.cwrap("QTS_NewSymbol", "number", ["number", "number", "number"]); | ||
@@ -70,2 +73,4 @@ this.QTS_GetSymbolDescriptionOrKey = this.module.cwrap("QTS_GetSymbolDescriptionOrKey", "number", ["number", "number"]); | ||
this.QTS_RuntimeDisableModuleLoader = this.module.cwrap("QTS_RuntimeDisableModuleLoader", null, ["number"]); | ||
this.QTS_bjson_encode = this.module.cwrap("QTS_bjson_encode", "number", ["number", "number"]); | ||
this.QTS_bjson_decode = this.module.cwrap("QTS_bjson_decode", "number", ["number", "number"]); | ||
} | ||
@@ -72,0 +77,0 @@ } |
@@ -39,2 +39,3 @@ import { QuickJSAsyncEmscriptenModule } from "../emscripten-types"; | ||
QTS_NewArray: (ctx: JSContextPointer) => JSValuePointer; | ||
QTS_NewArrayBuffer: (ctx: JSContextPointer, buffer: JSVoidPointer, length: number) => JSValuePointer; | ||
QTS_NewFloat64: (ctx: JSContextPointer, num: number) => JSValuePointer; | ||
@@ -44,2 +45,4 @@ QTS_GetFloat64: (ctx: JSContextPointer, value: JSValuePointer | JSValueConstPointer) => number; | ||
QTS_GetString: (ctx: JSContextPointer, value: JSValuePointer | JSValueConstPointer) => JSBorrowedCharPointer; | ||
QTS_GetArrayBuffer: (ctx: JSContextPointer, data: JSValuePointer | JSValueConstPointer) => JSVoidPointer; | ||
QTS_GetArrayBufferLength: (ctx: JSContextPointer, data: JSValuePointer | JSValueConstPointer) => number; | ||
QTS_NewSymbol: (ctx: JSContextPointer, description: BorrowedHeapCharPointer, isGlobal: number) => JSValuePointer; | ||
@@ -76,2 +79,4 @@ QTS_GetSymbolDescriptionOrKey: (ctx: JSContextPointer, value: JSValuePointer | JSValueConstPointer) => JSBorrowedCharPointer; | ||
QTS_RuntimeDisableModuleLoader: (rt: JSRuntimePointer) => void; | ||
QTS_bjson_encode: (ctx: JSContextPointer, val: JSValuePointer | JSValueConstPointer) => JSValuePointer; | ||
QTS_bjson_decode: (ctx: JSContextPointer, data: JSValuePointer | JSValueConstPointer) => JSValuePointer; | ||
} |
@@ -41,2 +41,3 @@ "use strict"; | ||
this.QTS_NewArray = this.module.cwrap("QTS_NewArray", "number", ["number"]); | ||
this.QTS_NewArrayBuffer = this.module.cwrap("QTS_NewArrayBuffer", "number", ["number", "number", "number"]); | ||
this.QTS_NewFloat64 = this.module.cwrap("QTS_NewFloat64", "number", ["number", "number"]); | ||
@@ -46,2 +47,4 @@ this.QTS_GetFloat64 = this.module.cwrap("QTS_GetFloat64", "number", ["number", "number"]); | ||
this.QTS_GetString = this.module.cwrap("QTS_GetString", "number", ["number", "number"]); | ||
this.QTS_GetArrayBuffer = this.module.cwrap("QTS_GetArrayBuffer", "number", ["number", "number"]); | ||
this.QTS_GetArrayBufferLength = this.module.cwrap("QTS_GetArrayBufferLength", "number", ["number", "number"]); | ||
this.QTS_NewSymbol = this.module.cwrap("QTS_NewSymbol", "number", ["number", "number", "number"]); | ||
@@ -78,2 +81,4 @@ this.QTS_GetSymbolDescriptionOrKey = (0, types_ffi_1.assertSync)(this.module.cwrap("QTS_GetSymbolDescriptionOrKey", "number", ["number", "number"])); | ||
this.QTS_RuntimeDisableModuleLoader = this.module.cwrap("QTS_RuntimeDisableModuleLoader", null, ["number"]); | ||
this.QTS_bjson_encode = this.module.cwrap("QTS_bjson_encode", "number", ["number", "number"]); | ||
this.QTS_bjson_decode = this.module.cwrap("QTS_bjson_decode", "number", ["number", "number"]); | ||
} | ||
@@ -80,0 +85,0 @@ } |
@@ -39,2 +39,3 @@ import { QuickJSEmscriptenModule } from "../emscripten-types"; | ||
QTS_NewArray: (ctx: JSContextPointer) => JSValuePointer; | ||
QTS_NewArrayBuffer: (ctx: JSContextPointer, buffer: JSVoidPointer, length: number) => JSValuePointer; | ||
QTS_NewFloat64: (ctx: JSContextPointer, num: number) => JSValuePointer; | ||
@@ -44,2 +45,4 @@ QTS_GetFloat64: (ctx: JSContextPointer, value: JSValuePointer | JSValueConstPointer) => number; | ||
QTS_GetString: (ctx: JSContextPointer, value: JSValuePointer | JSValueConstPointer) => JSBorrowedCharPointer; | ||
QTS_GetArrayBuffer: (ctx: JSContextPointer, data: JSValuePointer | JSValueConstPointer) => JSVoidPointer; | ||
QTS_GetArrayBufferLength: (ctx: JSContextPointer, data: JSValuePointer | JSValueConstPointer) => number; | ||
QTS_NewSymbol: (ctx: JSContextPointer, description: BorrowedHeapCharPointer, isGlobal: number) => JSValuePointer; | ||
@@ -69,2 +72,4 @@ QTS_GetSymbolDescriptionOrKey: (ctx: JSContextPointer, value: JSValuePointer | JSValueConstPointer) => JSBorrowedCharPointer; | ||
QTS_RuntimeDisableModuleLoader: (rt: JSRuntimePointer) => void; | ||
QTS_bjson_encode: (ctx: JSContextPointer, val: JSValuePointer | JSValueConstPointer) => JSValuePointer; | ||
QTS_bjson_decode: (ctx: JSContextPointer, data: JSValuePointer | JSValueConstPointer) => JSValuePointer; | ||
} |
@@ -40,2 +40,3 @@ "use strict"; | ||
this.QTS_NewArray = this.module.cwrap("QTS_NewArray", "number", ["number"]); | ||
this.QTS_NewArrayBuffer = this.module.cwrap("QTS_NewArrayBuffer", "number", ["number", "number", "number"]); | ||
this.QTS_NewFloat64 = this.module.cwrap("QTS_NewFloat64", "number", ["number", "number"]); | ||
@@ -45,2 +46,4 @@ this.QTS_GetFloat64 = this.module.cwrap("QTS_GetFloat64", "number", ["number", "number"]); | ||
this.QTS_GetString = this.module.cwrap("QTS_GetString", "number", ["number", "number"]); | ||
this.QTS_GetArrayBuffer = this.module.cwrap("QTS_GetArrayBuffer", "number", ["number", "number"]); | ||
this.QTS_GetArrayBufferLength = this.module.cwrap("QTS_GetArrayBufferLength", "number", ["number", "number"]); | ||
this.QTS_NewSymbol = this.module.cwrap("QTS_NewSymbol", "number", ["number", "number", "number"]); | ||
@@ -70,2 +73,4 @@ this.QTS_GetSymbolDescriptionOrKey = this.module.cwrap("QTS_GetSymbolDescriptionOrKey", "number", ["number", "number"]); | ||
this.QTS_RuntimeDisableModuleLoader = this.module.cwrap("QTS_RuntimeDisableModuleLoader", null, ["number"]); | ||
this.QTS_bjson_encode = this.module.cwrap("QTS_bjson_encode", "number", ["number", "number"]); | ||
this.QTS_bjson_decode = this.module.cwrap("QTS_bjson_decode", "number", ["number", "number"]); | ||
} | ||
@@ -72,0 +77,0 @@ } |
import { EitherModule } from "./emscripten-types"; | ||
import { OwnedHeapCharPointer, JSContextPointerPointer, JSValueConstPointerPointer, JSValuePointerPointer } from "./types-ffi"; | ||
import { OwnedHeapCharPointer, JSContextPointerPointer, JSValueConstPointerPointer, JSValuePointerPointer, JSVoidPointer } from "./types-ffi"; | ||
import { Lifetime } from "./lifetime"; | ||
@@ -8,2 +8,9 @@ import { QuickJSHandle } from "./types"; | ||
*/ | ||
type HeapUint8Array = { | ||
pointer: JSVoidPointer; | ||
numBytes: number; | ||
}; | ||
/** | ||
* @private | ||
*/ | ||
export declare class ModuleMemory { | ||
@@ -18,3 +25,5 @@ module: EitherModule; | ||
newHeapCharPointer(string: string): Lifetime<OwnedHeapCharPointer>; | ||
newHeapBufferPointer(buffer: Uint8Array): Lifetime<HeapUint8Array>; | ||
consumeHeapCharPointer(ptr: OwnedHeapCharPointer): string; | ||
} | ||
export {}; |
@@ -34,2 +34,8 @@ "use strict"; | ||
} | ||
newHeapBufferPointer(buffer) { | ||
const numBytes = buffer.byteLength; | ||
const ptr = this.module._malloc(numBytes); | ||
this.module.HEAPU8.set(buffer, ptr); | ||
return new lifetime_1.Lifetime({ pointer: ptr, numBytes }, undefined, (value) => this.module._free(value.pointer)); | ||
} | ||
consumeHeapCharPointer(ptr) { | ||
@@ -36,0 +42,0 @@ const str = this.module.UTF8ToString(ptr); |
{ | ||
"name": "quickjs-emscripten", | ||
"version": "0.23.0", | ||
"version": "0.24.0", | ||
"main": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "sideEffects": false, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 2 instances in 1 package
AI-detected potential code anomaly
Supply chain riskAI has identified unusual behaviors that may pose a security risk.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
9615710
105
21104
12
5