Socket
Socket
Sign inDemoInstall

quickjs-emscripten

Package Overview
Dependencies
Maintainers
1
Versions
59
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

quickjs-emscripten - npm Package Compare versions

Comparing version 0.3.2 to 0.4.0

4

dist/ffi.d.ts

@@ -64,2 +64,5 @@ import { QuickJSEmscriptenModule } from "./emscripten-types";

QTS_GetUndefined: () => JSValueConstPointer;
QTS_GetNull: () => JSValueConstPointer;
QTS_GetFalse: () => JSValueConstPointer;
QTS_GetTrue: () => JSValueConstPointer;
QTS_NewRuntime: () => JSRuntimePointer;

@@ -73,2 +76,3 @@ QTS_FreeRuntime: (rt: JSRuntimePointer) => void;

QTS_NewObjectProto: (ctx: JSContextPointer, proto: JSValuePointer | JSValueConstPointer) => JSValuePointer;
QTS_NewArray: (ctx: JSContextPointer) => JSValuePointer;
QTS_NewFloat64: (ctx: JSContextPointer, num: number) => JSValuePointer;

@@ -75,0 +79,0 @@ QTS_GetFloat64: (ctx: JSContextPointer, value: JSValuePointer | JSValueConstPointer) => number;

@@ -22,2 +22,5 @@ "use strict";

this.QTS_GetUndefined = this.module.cwrap("QTS_GetUndefined", "number", []);
this.QTS_GetNull = this.module.cwrap("QTS_GetNull", "number", []);
this.QTS_GetFalse = this.module.cwrap("QTS_GetFalse", "number", []);
this.QTS_GetTrue = this.module.cwrap("QTS_GetTrue", "number", []);
this.QTS_NewRuntime = this.module.cwrap("QTS_NewRuntime", "number", []);

@@ -31,2 +34,3 @@ this.QTS_FreeRuntime = this.module.cwrap("QTS_FreeRuntime", null, ["number"]);

this.QTS_NewObjectProto = this.module.cwrap("QTS_NewObjectProto", "number", ["number", "number"]);
this.QTS_NewArray = this.module.cwrap("QTS_NewArray", "number", ["number"]);
this.QTS_NewFloat64 = this.module.cwrap("QTS_NewFloat64", "number", ["number", "number"]);

@@ -33,0 +37,0 @@ this.QTS_GetFloat64 = this.module.cwrap("QTS_GetFloat64", "number", ["number", "number"]);

@@ -13,2 +13,3 @@ import { QuickJSFFI, JSContextPointer, JSValuePointer, JSRuntimePointer, JSValueConstPointer } from './ffi';

export declare type ShouldInterruptHandler = (vm: QuickJSVm) => boolean | undefined;
export declare type QuickJSPropertyKey = number | string | QuickJSHandle;
/**

@@ -67,3 +68,3 @@ * A lifetime prevents access to a value after the lifetime has been

*
* Create QuickJS values with methods like [[newNumber]], [[newString]], [[newObject]], and [[newFunction]].
* Create QuickJS values with methods like [[newNumber]], [[newString]], [[newArray]], [[newObject]], and [[newFunction]].
*

@@ -79,2 +80,5 @@ * Call [[setProp]] or [[defineProp]] to customize objects. Use those methods with [[global]] to expose the

private _undefined;
private _null;
private _false;
private _true;
private _global;

@@ -96,2 +100,14 @@ private readonly lifetimes;

/**
* [`null`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null).
*/
get null(): QuickJSHandle;
/**
* [`true`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/true).
*/
get true(): QuickJSHandle;
/**
* [`false`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/false).
*/
get false(): QuickJSHandle;
/**
* [`global`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects).

@@ -134,2 +150,7 @@ * A handle to the global object inside the interpreter.

/**
* `[]`.
* Create a new QuickJS [array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array).
*/
newArray(): QuickJSHandle;
/**
* Convert a Javascript function into a QuickJS function value.

@@ -150,3 +171,3 @@ * See [[VmFunctionImplementation]] for more details.

*/
getProp(handle: QuickJSHandle, key: string | QuickJSHandle): QuickJSHandle;
getProp(handle: QuickJSHandle, key: QuickJSPropertyKey): QuickJSHandle;
/**

@@ -161,5 +182,5 @@ * `handle[key] = value`.

* @param key - The property may be specified as a JSValue handle, or as a
* Javascript string (which will be converted automatically).
* Javascript string or number (which will be converted automatically to a JSValue).
*/
setProp(handle: QuickJSHandle, key: string | QuickJSHandle, value: QuickJSHandle): void;
setProp(handle: QuickJSHandle, key: QuickJSPropertyKey, value: QuickJSHandle): void;
/**

@@ -169,5 +190,5 @@ * [`Object.defineProperty(handle, key, descriptor)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty).

* @param key - The property may be specified as a JSValue handle, or as a
* Javascript string (which will be converted automatically).
* Javascript string or number (which will be converted automatically to a JSValue).
*/
defineProp(handle: QuickJSHandle, key: string | QuickJSHandle, descriptor: VmPropertyDescriptor<QuickJSHandle>): void;
defineProp(handle: QuickJSHandle, key: QuickJSPropertyKey, descriptor: VmPropertyDescriptor<QuickJSHandle>): void;
/**

@@ -241,2 +262,3 @@ * [`func.call(thisVal, ...args)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call).

private heapValueHandle;
private borrowPropertyKey;
private toPointerArray;

@@ -243,0 +265,0 @@ private errorToHandle;

@@ -160,3 +160,3 @@ "use strict";

*
* Create QuickJS values with methods like [[newNumber]], [[newString]], [[newObject]], and [[newFunction]].
* Create QuickJS values with methods like [[newNumber]], [[newString]], [[newArray]], [[newObject]], and [[newFunction]].
*

@@ -173,2 +173,5 @@ * Call [[setProp]] or [[defineProp]] to customize objects. Use those methods with [[global]] to expose the

this._undefined = undefined;
this._null = undefined;
this._false = undefined;
this._true = undefined;
this._global = undefined;

@@ -259,2 +262,47 @@ this.lifetimes = [];

});
Object.defineProperty(QuickJSVm.prototype, "null", {
/**
* [`null`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null).
*/
get: function () {
if (this._null) {
return this._null;
}
// Null is a constant, immutable value in QuickJS.
var ptr = this.ffi.QTS_GetNull();
return (this._null = new StaticLifetime(ptr));
},
enumerable: true,
configurable: true
});
Object.defineProperty(QuickJSVm.prototype, "true", {
/**
* [`true`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/true).
*/
get: function () {
if (this._true) {
return this._true;
}
// True is a constant, immutable value in QuickJS.
var ptr = this.ffi.QTS_GetTrue();
return (this._true = new StaticLifetime(ptr));
},
enumerable: true,
configurable: true
});
Object.defineProperty(QuickJSVm.prototype, "false", {
/**
* [`false`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/false).
*/
get: function () {
if (this._false) {
return this._false;
}
// False is a constant, immutable value in QuickJS.
var ptr = this.ffi.QTS_GetFalse();
return (this._false = new StaticLifetime(ptr));
},
enumerable: true,
configurable: true
});
Object.defineProperty(QuickJSVm.prototype, "global", {

@@ -337,2 +385,10 @@ /**

/**
* `[]`.
* Create a new QuickJS [array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array).
*/
QuickJSVm.prototype.newArray = function () {
var ptr = this.ffi.QTS_NewArray(this.ctx.value);
return this.heapValueHandle(ptr);
};
/**
* Convert a Javascript function into a QuickJS function value.

@@ -364,9 +420,8 @@ * See [[VmFunctionImplementation]] for more details.

this.assertOwned(handle);
var quickJSKey = typeof key === 'string' ? this.newString(key) : key;
var quickJSKey = this.borrowPropertyKey(key);
var ptr = this.ffi.QTS_GetProp(this.ctx.value, handle.value, quickJSKey.value);
var result = this.heapValueHandle(ptr);
if (typeof key === 'string') {
// we allocated a string
quickJSKey.dispose();
}
// free newly allocated value if key was a string or number. No-op if string was already
// a QuickJS handle.
quickJSKey.dispose();
return result;

@@ -383,12 +438,11 @@ };

* @param key - The property may be specified as a JSValue handle, or as a
* Javascript string (which will be converted automatically).
* Javascript string or number (which will be converted automatically to a JSValue).
*/
QuickJSVm.prototype.setProp = function (handle, key, value) {
this.assertOwned(handle);
var quickJSKey = typeof key === 'string' ? this.newString(key) : key;
var quickJSKey = this.borrowPropertyKey(key);
this.ffi.QTS_SetProp(this.ctx.value, handle.value, quickJSKey.value, value.value);
if (typeof key === 'string') {
// we allocated a string
quickJSKey.dispose();
}
// free newly allocated value if key was a string or number. No-op if string was already
// a QuickJS handle.
quickJSKey.dispose();
};

@@ -399,7 +453,7 @@ /**

* @param key - The property may be specified as a JSValue handle, or as a
* Javascript string (which will be converted automatically).
* Javascript string or number (which will be converted automatically to a JSValue).
*/
QuickJSVm.prototype.defineProp = function (handle, key, descriptor) {
this.assertOwned(handle);
var quickJSKey = typeof key === 'string' ? this.newString(key) : key;
var quickJSKey = this.borrowPropertyKey(key);
var value = descriptor.value || this.undefined;

@@ -416,6 +470,5 @@ var configurable = Boolean(descriptor.configurable);

this.ffi.QTS_DefineProp(this.ctx.value, handle.value, quickJSKey.value, value.value, get.value, set.value, configurable, enumerable, hasValue);
if (typeof key === 'string') {
// we allocated a string
quickJSKey.dispose();
}
// free newly allocated value if key was a string or number. No-op if string was already
// a QuickJS handle.
quickJSKey.dispose();
// dispose created functions; or no-op if these are this.undefined

@@ -566,2 +619,13 @@ get.dispose();

};
QuickJSVm.prototype.borrowPropertyKey = function (key) {
if (typeof key === 'number') {
return this.newNumber(key);
}
if (typeof key === 'string') {
return this.newString(key);
}
// key is alerady a JSValue, but we're borrowing it. Return a static handle
// for intenal use only.
return new StaticLifetime(key.value, undefined, this);
};
QuickJSVm.prototype.toPointerArray = function (handleArray) {

@@ -568,0 +632,0 @@ var _this = this;

@@ -85,2 +85,11 @@ "use strict";

});
mocha_1.it('can round-trip true', function () {
assert_1.default.strictEqual(vm.dump(vm.true), true);
});
mocha_1.it('can round-trip false', function () {
assert_1.default.strictEqual(vm.dump(vm.false), false);
});
mocha_1.it('can round-trip null', function () {
assert_1.default.strictEqual(vm.dump(vm.null), null);
});
});

@@ -182,2 +191,25 @@ mocha_1.describe('functions', function () {

});
mocha_1.describe('arrays', function () {
mocha_1.it('can set and get entries by native number', function () {
var array = vm.newArray();
var val1 = vm.newNumber(101);
vm.setProp(array, 0, val1);
var val2 = vm.getProp(array, 0);
assert_1.default.strictEqual(vm.getNumber(val2), 101);
array.dispose();
val1.dispose();
val2.dispose();
});
mocha_1.it('adding items sets array.length', function () {
var vals = [vm.newNumber(0), vm.newNumber(1), vm.newString('cow')];
var array = vm.newArray();
for (var i = 0; i < vals.length; i++) {
vm.setProp(array, i, vals[i]);
}
var length = vm.getProp(array, 'length');
assert_1.default.strictEqual(vm.getNumber(length), 3);
array.dispose();
vals.forEach(function (val) { return val.dispose(); });
});
});
mocha_1.describe('.unwrapResult', function () {

@@ -184,0 +216,0 @@ mocha_1.it('successful result: returns the value', function () {

4

package.json
{
"name": "quickjs-emscripten",
"version": "0.3.2",
"version": "0.4.0",
"main": "dist/quickjs.js",

@@ -27,3 +27,3 @@ "license": "MIT",

"tarball": "yarn pack --filename build/quickjs-emscripten.tgz",
"smoketest": "cd ../quickjs-emscripten-examples && yarn add ../quickjs-emscripten/build/quickjs-emscripten.tgz && tsc --lib ES2015,dom index.ts && node index.js"
"smoketest": "cd examples/typescript-smoketest && yarn add ../../build/quickjs-emscripten.tgz && tsc --lib ES2015,dom index.ts && node index.js"
},

@@ -30,0 +30,0 @@ "devDependencies": {

@@ -6,5 +6,5 @@ # quickjs-emscripten

* Safely evaluate untrusted Javascript (up to ES2020).
* Create and manipulate values inside the QuickJS runtime.
* Expose host functions to the QuickJS runtime.
- Safely evaluate untrusted Javascript (up to ES2020).
- Create and manipulate values inside the QuickJS runtime.
- Expose host functions to the QuickJS runtime.

@@ -54,3 +54,3 @@ ```typescript

const result = QuickJS.evalCode('1 + 1', {
shouldInterrupt: shouldInterruptAfterDeadline(Date.now() + 1000)
shouldInterrupt: shouldInterruptAfterDeadline(Date.now() + 1000),
})

@@ -84,4 +84,4 @@ console.log(result)

* [API Documentation](https://github.com/justjake/quickjs-emscripten/blob/master/doc/globals.md)
* [Examples](https://github.com/justjake/quickjs-emscripten/blob/master/ts/quickjs.test.ts)
- [API Documentation](https://github.com/justjake/quickjs-emscripten/blob/master/doc/globals.md)
- [Examples](https://github.com/justjake/quickjs-emscripten/blob/master/ts/quickjs.test.ts)

@@ -105,3 +105,2 @@ ## Background

Ideas for future work:

@@ -108,0 +107,0 @@

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc