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.6.1 to 0.7.0

dist/result.d.ts

3

dist/ffi.d.ts

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

QTS_RuntimeDisableInterruptHandler: (rt: JSRuntimePointer) => void;
QTS_RuntimeSetMemoryLimit: (rt: JSRuntimePointer, limit: number) => void;
QTS_RuntimeComputeMemoryUsage: (rt: JSRuntimePointer, ctx: JSContextPointer) => JSValuePointer;
QTS_RuntimeDumpMemoryUsage: (rt: JSRuntimePointer) => string;
QTS_GetUndefined: () => JSValueConstPointer;

@@ -65,0 +68,0 @@ QTS_GetNull: () => JSValueConstPointer;

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

this.QTS_RuntimeDisableInterruptHandler = this.module.cwrap("QTS_RuntimeDisableInterruptHandler", null, ["number"]);
this.QTS_RuntimeSetMemoryLimit = this.module.cwrap("QTS_RuntimeSetMemoryLimit", null, ["number", "number"]);
this.QTS_RuntimeComputeMemoryUsage = this.module.cwrap("QTS_RuntimeComputeMemoryUsage", "number", ["number", "number"]);
this.QTS_RuntimeDumpMemoryUsage = this.module.cwrap("QTS_RuntimeDumpMemoryUsage", "string", ["number"]);
this.QTS_GetUndefined = this.module.cwrap("QTS_GetUndefined", "number", []);

@@ -23,0 +26,0 @@ this.QTS_GetNull = this.module.cwrap("QTS_GetNull", "number", []);

@@ -24,7 +24,8 @@ import { QuickJSFFI, JSContextPointer, JSValuePointer, JSRuntimePointer, JSValueConstPointer } from './ffi';

*/
export declare class Lifetime<T, Owner = never> {
private readonly _value;
private readonly disposer?;
private readonly _owner?;
private _alive;
export declare class Lifetime<T, TCopy = never, Owner = never> {
protected readonly _value: T;
protected readonly copier?: ((value: T | TCopy) => TCopy) | undefined;
protected readonly disposer?: ((value: T | TCopy) => void) | undefined;
protected readonly _owner?: Owner | undefined;
protected _alive: boolean;
/**

@@ -38,3 +39,3 @@ * When the Lifetime is disposed, it will call `disposer(_value)`. Use the

*/
constructor(_value: T, disposer?: ((value: T) => void) | undefined, _owner?: Owner | undefined);
constructor(_value: T, copier?: ((value: T | TCopy) => TCopy) | undefined, disposer?: ((value: T | TCopy) => void) | undefined, _owner?: Owner | undefined);
get alive(): boolean;

@@ -49,3 +50,8 @@ /**

get owner(): Owner | undefined;
get dupable(): boolean;
/**
* Create a new handle pointing to the same [[value]].
*/
dup(): Lifetime<TCopy, TCopy, Owner>;
/**
* Dispose of [[value]] and perform cleanup.

@@ -59,6 +65,20 @@ */

*/
export declare class StaticLifetime<T, Owner = never> extends Lifetime<T, Owner> {
export declare class StaticLifetime<T, Owner = never> extends Lifetime<T, T, Owner> {
constructor(value: T, owner?: Owner);
get dupable(): boolean;
dup(): this;
dispose(): void;
}
/**
* A Lifetime that does not own its `value`. A WeakLifetime never calls its
* `disposer` function, but can be `dup`ed to produce regular lifetimes that
* do.
*
* Used for function arguments.
*/
export declare class WeakLifetime<T, TCopy = never, Owner = never> extends Lifetime<T, TCopy, Owner> {
constructor(value: T, copier?: (value: T | TCopy) => TCopy, disposer?: (value: TCopy) => void, owner?: Owner);
dispose(): void;
}
/**
* Used as an optional for the results of executing pendingJobs.

@@ -277,2 +297,20 @@ * On success, `value` contains the number of async jobs executed

/**
* Set the max memory this runtime can allocate.
* To remove the limit, set to `-1`.
*/
setMemoryLimit(limitBytes: number): void;
/**
* Compute memory usage for this runtime. Returns the result as a handle to a
* JSValue object. Use [[dump]] to convert to a native object.
* Calling this method will allocate more memory inside the runtime. The information
* is accurate as of just before the call to `computeMemoryUsage`.
* For a human-digestable representation, see [[dumpMemoryUsage]].
*/
computeMemoryUsage(): QuickJSHandle;
/**
* @returns a human-readable description of memory usage in this runtime.
* For programatic access to this information, see [[computeMemoryUsage]].
*/
dumpMemoryUsage(): string;
/**
* Remove the interrupt handler, if any.

@@ -298,2 +336,3 @@ * See [[setShouldInterruptHandler]].

private assertOwned;
private copyJSValue;
private freeJSValue;

@@ -309,3 +348,3 @@ private heapValueHandle;

*/
export declare type StaticJSValue = StaticLifetime<JSValueConstPointer>;
export declare type StaticJSValue = Lifetime<JSValueConstPointer, JSValueConstPointer, QuickJSVm>;
/**

@@ -321,3 +360,3 @@ * A QuickJSHandle to a borrowed value that does not need to be disposed.

*/
export declare type JSValueConst = Lifetime<JSValueConstPointer, QuickJSVm>;
export declare type JSValueConst = Lifetime<JSValueConstPointer, JSValuePointer, QuickJSVm>;
/**

@@ -338,3 +377,3 @@ * A owned QuickJSHandle that should be disposed or returned.

*/
export declare type JSValue = Lifetime<JSValuePointer, QuickJSVm>;
export declare type JSValue = Lifetime<JSValuePointer, JSValuePointer, QuickJSVm>;
/**

@@ -341,0 +380,0 @@ * Wraps a C pointer to a QuickJS JSValue, which represents a Javascript value inside

@@ -89,4 +89,5 @@ "use strict";

*/
function Lifetime(_value, disposer, _owner) {
function Lifetime(_value, copier, disposer, _owner) {
this._value = _value;
this.copier = copier;
this.disposer = disposer;

@@ -124,3 +125,20 @@ this._owner = _owner;

});
Object.defineProperty(Lifetime.prototype, "dupable", {
get: function () {
return !!this.copier;
},
enumerable: true,
configurable: true
});
/**
* Create a new handle pointing to the same [[value]].
*/
Lifetime.prototype.dup = function () {
this.assertAlive();
if (!this.copier) {
throw new Error('Non-dupable lifetime');
}
return new Lifetime(this.copier(this._value), this.copier, this.disposer, this._owner);
};
/**
* Dispose of [[value]] and perform cleanup.

@@ -148,5 +166,17 @@ */

__extends(StaticLifetime, _super);
function StaticLifetime() {
return _super !== null && _super.apply(this, arguments) || this;
function StaticLifetime(value, owner) {
return _super.call(this, value, undefined, undefined, owner) || this;
}
Object.defineProperty(StaticLifetime.prototype, "dupable", {
// Static lifetime doesn't need a copier to be copiable
get: function () {
return true;
},
enumerable: true,
configurable: true
});
// Copy returns the same instance.
StaticLifetime.prototype.dup = function () {
return this;
};
// Dispose does nothing.

@@ -158,2 +188,21 @@ StaticLifetime.prototype.dispose = function () { };

/**
* A Lifetime that does not own its `value`. A WeakLifetime never calls its
* `disposer` function, but can be `dup`ed to produce regular lifetimes that
* do.
*
* Used for function arguments.
*/
var WeakLifetime = /** @class */ (function (_super) {
__extends(WeakLifetime, _super);
function WeakLifetime(value, copier, disposer, owner) {
// We don't care if the disposer doesn't support freeing T
return _super.call(this, value, copier, disposer, owner) || this;
}
WeakLifetime.prototype.dispose = function () {
this._alive = false;
};
return WeakLifetime;
}(Lifetime));
exports.WeakLifetime = WeakLifetime;
/**
* QuickJSVm wraps a QuickJS Javascript runtime (JSRuntime*) and context (JSContext*).

@@ -204,7 +253,7 @@ * This class's methods return {@link QuickJSHandle}, which wrap C pointers (JSValue*).

}
var thisHandle = new Lifetime(this_ptr, undefined, _this);
var thisHandle = new WeakLifetime(this_ptr, _this.copyJSValue, _this.freeJSValue, _this);
var argHandles = new Array(argc);
for (var i = 0; i < argc; i++) {
var ptr = _this.ffi.QTS_ArgvGetJSValueConstPointer(argv, i);
argHandles[i] = new Lifetime(ptr, undefined, _this);
argHandles[i] = new WeakLifetime(ptr, _this.copyJSValue, _this.freeJSValue, _this);
}

@@ -251,2 +300,5 @@ var ownedResultPtr = 0;

};
this.copyJSValue = function (ptr) {
return _this.ffi.QTS_DupValue(_this.ctx.value, ptr);
};
this.freeJSValue = function (ptr) {

@@ -336,5 +388,5 @@ _this.ffi.QTS_FreeValuePointer(_this.ctx.value, ptr);

// This isn't technically a static lifetime, but since it has the same
// lifetime as the VM, it's okay to fake one since when the VM iss
// lifetime as the VM, it's okay to fake one since when the VM is
// disposed, no other functions will accept the value.
this._global = new StaticLifetime(ptr, undefined, this);
this._global = new StaticLifetime(ptr, this);
return this._global;

@@ -635,2 +687,29 @@ },

/**
* Set the max memory this runtime can allocate.
* To remove the limit, set to `-1`.
*/
QuickJSVm.prototype.setMemoryLimit = function (limitBytes) {
if (limitBytes < 0 && limitBytes !== -1) {
throw new Error('Cannot set memory limit to negative number. To unset, pass -1');
}
this.ffi.QTS_RuntimeSetMemoryLimit(this.rt.value, limitBytes);
};
/**
* Compute memory usage for this runtime. Returns the result as a handle to a
* JSValue object. Use [[dump]] to convert to a native object.
* Calling this method will allocate more memory inside the runtime. The information
* is accurate as of just before the call to `computeMemoryUsage`.
* For a human-digestable representation, see [[dumpMemoryUsage]].
*/
QuickJSVm.prototype.computeMemoryUsage = function () {
return this.heapValueHandle(this.ffi.QTS_RuntimeComputeMemoryUsage(this.rt.value, this.ctx.value));
};
/**
* @returns a human-readable description of memory usage in this runtime.
* For programatic access to this information, see [[computeMemoryUsage]].
*/
QuickJSVm.prototype.dumpMemoryUsage = function () {
return this.ffi.QTS_RuntimeDumpMemoryUsage(this.rt.value);
};
/**
* Remove the interrupt handler, if any.

@@ -667,3 +746,3 @@ * See [[setShouldInterruptHandler]].

QuickJSVm.prototype.heapValueHandle = function (ptr) {
return new Lifetime(ptr, this.freeJSValue, this);
return new Lifetime(ptr, this.copyJSValue, this.freeJSValue, this);
};

@@ -679,3 +758,3 @@ QuickJSVm.prototype.borrowPropertyKey = function (key) {

// for intenal use only.
return new StaticLifetime(key.value, undefined, this);
return new StaticLifetime(key.value, this);
};

@@ -689,3 +768,3 @@ QuickJSVm.prototype.toPointerArray = function (handleArray) {

heapBytes.set(new Uint8Array(typedArray.buffer));
return new Lifetime(ptr, function (ptr) { return _this.module._free(ptr); });
return new Lifetime(ptr, undefined, function (ptr) { return _this.module._free(ptr); });
};

@@ -799,7 +878,7 @@ QuickJSVm.prototype.errorToHandle = function (error) {

var _this = this;
var rt = new Lifetime(this.ffi.QTS_NewRuntime(), function (rt_ptr) {
var rt = new Lifetime(this.ffi.QTS_NewRuntime(), undefined, function (rt_ptr) {
_this.rtMap.delete(rt_ptr);
_this.ffi.QTS_FreeRuntime(rt_ptr);
});
var ctx = new Lifetime(this.ffi.QTS_NewContext(rt.value), function (ctx_ptr) {
var ctx = new Lifetime(this.ffi.QTS_NewContext(rt.value), undefined, function (ctx_ptr) {
_this.vmMap.delete(ctx_ptr);

@@ -806,0 +885,0 @@ _this.ffi.QTS_FreeContext(ctx_ptr);

@@ -64,9 +64,6 @@ "use strict";

}); });
afterEach(function () { return __awaiter(void 0, void 0, void 0, function () {
return __generator(this, function (_a) {
vm.dispose();
vm = undefined;
return [2 /*return*/];
});
}); });
afterEach(function () {
vm.dispose();
vm = undefined;
});
mocha_1.describe('primitives', function () {

@@ -134,2 +131,17 @@ mocha_1.it('can round-trip a number', function () {

});
mocha_1.it('can see its arguments being cloned', function () {
var value;
var fnHandle = vm.newFunction('doSomething', function (arg) {
value = arg.dup();
});
var argHandle = vm.newString('something');
var callHandle = vm.callFunction(fnHandle, vm.undefined, argHandle);
argHandle.dispose();
vm.unwrapResult(callHandle).dispose();
if (!value)
throw new Error("Value unset");
assert_1.default.equal(vm.getString(value), 'something');
value.dispose();
fnHandle.dispose();
});
});

@@ -385,2 +397,64 @@ mocha_1.describe('properties', function () {

});
mocha_1.describe('.computeMemoryUsage', function () {
mocha_1.it('returns an object with JSON memory usage info', function () {
var result = vm.computeMemoryUsage();
var resultObj = vm.dump(result);
result.dispose();
// Is this test failing? Just commit the new value as long as it seems reasonable.
assert_1.default.deepEqual(resultObj, {
array_count: 1,
atom_count: 414,
atom_size: 13593,
binary_object_count: 0,
binary_object_size: 0,
c_func_count: 46,
fast_array_count: 1,
fast_array_elements: 0,
js_func_code_size: 0,
js_func_count: 0,
js_func_pc2line_count: 0,
js_func_pc2line_size: 0,
js_func_size: 0,
malloc_count: 665,
malloc_limit: 4294967295,
memory_used_count: 665,
memory_used_size: 36305,
obj_count: 97,
obj_size: 4656,
prop_count: 654,
prop_size: 5936,
shape_count: 50,
shape_size: 10328,
str_count: 0,
str_size: 0,
});
});
});
mocha_1.describe('.setMemoryLimit', function () {
mocha_1.it('sets an enforced limit', function () {
vm.setMemoryLimit(100);
var result = vm.evalCode("new Uint8Array(101); \"ok\"");
if (!result.error) {
result.value.dispose();
throw new Error('should be an error');
}
vm.setMemoryLimit(-1); // so we can dump
var error = vm.dump(result.error);
result.error.dispose();
assert_1.default.strictEqual(error, null);
});
mocha_1.it('removes limit when set to -1', function () {
vm.setMemoryLimit(100);
vm.setMemoryLimit(-1);
var result = vm.unwrapResult(vm.evalCode("new Uint8Array(101); \"ok\""));
var value = vm.dump(result);
result.dispose();
assert_1.default.strictEqual(value, 'ok');
});
});
mocha_1.describe('.dumpMemoryUsage()', function () {
mocha_1.it('logs memory usage', function () {
assert_1.default(vm.dumpMemoryUsage().endsWith('per fast array)\n'), 'should end with "per fast array)\\n"');
});
});
return [2 /*return*/];

@@ -387,0 +461,0 @@ });

13

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

@@ -26,6 +26,6 @@ "license": "MIT",

"scripts": {
"prepare": "yarn make-release && yarn build && mocha ./dist/quickjs.test.js && yarn tarball && yarn smoketest-node",
"prepare": "yarn prepack && mocha ./dist/quickjs.test.js && yarn tarball && yarn smoketest-node",
"update-quickjs": "git subtree pull --prefix=quickjs --squash git@github.com:ldarren/QuickJS.git mod",
"make-debug": "make DEBUG=1 -j 8",
"make-release": "which emcc && make clean && make -j 8",
"make-release": "make clean && make -j 8",
"clean": "make clean",

@@ -38,5 +38,6 @@ "build": "tsc",

"run-n": "yarn make-debug && ./build/wrapper/native/test.exe",
"prepack": "yarn make-release && yarn build",
"tarball": "mkdir -p build && yarn pack --filename build/quickjs-emscripten.tgz",
"smoketest-node": "cd examples/typescript-smoketest && yarn add ../../build/quickjs-emscripten.tgz --update-checksums && tsc --lib ES2015,dom index.ts && node index.js",
"smoketest-cra": "cd examples/website && yarn add ../../build/quickjs-emscripten.tgz --update-checksums && yarn build"
"smoketest-node": "cd examples/typescript-smoketest && YARN_CHECKSUM_BEHAVIOR=update yarn add quickjs-emscripten@../../build/quickjs-emscripten.tgz && tsc --lib ES2015,dom index.ts && node index.js",
"smoketest-cra": "cd examples/website && YARN_CHECKSUM_BEHAVIOR=update yarn add quickjs-emscripten@../../build/quickjs-emscripten.tgz && yarn build"
},

@@ -47,2 +48,4 @@ "devDependencies": {

"@types/node": "^13.1.4",
"embin-darwin": "1.39.19-4",
"embin-linux": "1.39.19-4",
"mocha": "7.2.0",

@@ -49,0 +52,0 @@ "prettier": "^1.19.1",

@@ -131,4 +131,3 @@ # quickjs-emscripten

This library is implemented in two languages: C (compiled to WASM with
Emscripten), and Typescript. Emscripten outputs are checked in, so you will
only need the C compiler if you need to modify C code.
Emscripten), and Typescript.

@@ -146,6 +145,6 @@ ### The C parts

This project uses `emscripten 1.39.18`. First, install `emsdk` by following the official instructions here:
https://emscripten.org/docs/getting_started/downloads.html#installation-instructions.
Once you have `emsdk`, install emcc version 1.39.19 with `emsdk install 1.39.18`.
Your `emcc -v` output should look a little like this:
This project uses `emscripten 1.39.19`. The install should be handled automatically
if you're working from Linux or OSX (if using Windows, the best is to use WSL to work
on this repository). If everything is right, running `yarn embin emcc -v` should print
something like this:

@@ -175,1 +174,5 @@ ```

- `yarn test --watch` watches for changes and re-runs the tests.
### Yarn updates
Just run `yarn set version from sources` to upgrade the Yarn release.

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