@gjsify/vm 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,26 @@
1
+ # @gjsify/vm
2
+
3
+ GJS stub implementation of the Node.js `vm` module. Provides runInThisContext via eval and Script.
4
+
5
+ Part of the [gjsify](https://github.com/gjsify/gjsify) project — Node.js and Web APIs for GJS (GNOME JavaScript).
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install @gjsify/vm
11
+ # or
12
+ yarn add @gjsify/vm
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ```typescript
18
+ import { runInThisContext, Script } from '@gjsify/vm';
19
+
20
+ const result = runInThisContext('1 + 2');
21
+ console.log(result); // 3
22
+ ```
23
+
24
+ ## License
25
+
26
+ MIT
@@ -0,0 +1,68 @@
1
+ const contextSymbol = /* @__PURE__ */ Symbol("vm.context");
2
+ function runInThisContext(code, _options) {
3
+ return eval(code);
4
+ }
5
+ function runInNewContext(code2, context, _options2) {
6
+ const sandbox = context || {};
7
+ const keys = Object.keys(sandbox);
8
+ const values = keys.map((k) => sandbox[k]);
9
+ const fn = new Function(...keys, `return eval(${JSON.stringify(code2)})`);
10
+ return fn(...values);
11
+ }
12
+ function runInContext(code2, context, _options2) {
13
+ return runInNewContext(code2, context);
14
+ }
15
+ function createContext(context) {
16
+ const ctx = context || {};
17
+ Object.defineProperty(ctx, contextSymbol, { value: true, enumerable: false });
18
+ return ctx;
19
+ }
20
+ function isContext(context) {
21
+ if (typeof context !== "object" || context === null) {
22
+ throw new TypeError(
23
+ `The "object" argument must be of type object. Received ${context === null ? "null" : `type ${typeof context}`}`
24
+ );
25
+ }
26
+ return context[contextSymbol] === true;
27
+ }
28
+ function compileFunction(code2, params, _options2) {
29
+ const paramNames = params || [];
30
+ return new Function(...paramNames, code2);
31
+ }
32
+ class Script {
33
+ _code;
34
+ constructor(code2, _options2) {
35
+ this._code = code2;
36
+ }
37
+ runInThisContext(_options) {
38
+ return eval(this._code);
39
+ }
40
+ runInNewContext(context, _options2) {
41
+ return runInNewContext(this._code, context);
42
+ }
43
+ runInContext(context, _options2) {
44
+ return runInNewContext(this._code, context);
45
+ }
46
+ createCachedData() {
47
+ return new Uint8Array(0);
48
+ }
49
+ }
50
+ var index_default = {
51
+ runInThisContext,
52
+ runInNewContext,
53
+ runInContext,
54
+ createContext,
55
+ isContext,
56
+ compileFunction,
57
+ Script
58
+ };
59
+ export {
60
+ Script,
61
+ compileFunction,
62
+ createContext,
63
+ index_default as default,
64
+ isContext,
65
+ runInContext,
66
+ runInNewContext,
67
+ runInThisContext
68
+ };
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Run code in the current V8/SpiderMonkey context.
3
+ * Equivalent to eval() but matches Node.js vm.runInThisContext() API.
4
+ */
5
+ export declare function runInThisContext(code: string, _options?: Record<string, unknown>): unknown;
6
+ /**
7
+ * Run code with a sandbox object providing global-like variables.
8
+ * Uses Function constructor to inject sandbox properties as local variables.
9
+ * NOTE: This is NOT a security sandbox — code can still access globalThis.
10
+ * This matches Node.js vm module behavior which also does not provide true isolation.
11
+ */
12
+ export declare function runInNewContext(code: string, context?: Record<string, unknown>, _options?: Record<string, unknown>): unknown;
13
+ /**
14
+ * Run code in a previously created context.
15
+ * Since we don't have real VM contexts, this delegates to runInNewContext.
16
+ */
17
+ export declare function runInContext(code: string, context: Record<string, unknown>, _options?: Record<string, unknown>): unknown;
18
+ /**
19
+ * Create a "context" object. Marks it with a symbol so isContext() works.
20
+ * In real Node.js, this creates a V8 Context. Here it just marks an object.
21
+ */
22
+ export declare function createContext(context?: Record<string, unknown>): Record<string, unknown>;
23
+ /**
24
+ * Check if an object was created by createContext().
25
+ * Throws TypeError for non-object arguments, matching Node.js behavior.
26
+ */
27
+ export declare function isContext(context: unknown): boolean;
28
+ /**
29
+ * Compile a function from source code with optional parameters and context.
30
+ * Matches Node.js vm.compileFunction() API surface.
31
+ * eslint-disable-next-line no-new-func — intentional: vm module implements code evaluation
32
+ */
33
+ export declare function compileFunction(code: string, params?: string[], _options?: {
34
+ parsingContext?: Record<string, unknown>;
35
+ contextExtensions?: object[];
36
+ }): Function;
37
+ /**
38
+ * Script class — compiles code for repeated execution.
39
+ */
40
+ export declare class Script {
41
+ private _code;
42
+ constructor(code: string, _options?: Record<string, unknown>);
43
+ runInThisContext(_options?: Record<string, unknown>): unknown;
44
+ runInNewContext(context?: Record<string, unknown>, _options?: Record<string, unknown>): unknown;
45
+ runInContext(context: Record<string, unknown>, _options?: Record<string, unknown>): unknown;
46
+ createCachedData(): Uint8Array;
47
+ }
48
+ declare const _default: {
49
+ runInThisContext: typeof runInThisContext;
50
+ runInNewContext: typeof runInNewContext;
51
+ runInContext: typeof runInContext;
52
+ createContext: typeof createContext;
53
+ isContext: typeof isContext;
54
+ compileFunction: typeof compileFunction;
55
+ Script: typeof Script;
56
+ };
57
+ export default _default;
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@gjsify/vm",
3
+ "version": "0.1.0",
4
+ "description": "Node.js vm module for Gjs",
5
+ "type": "module",
6
+ "module": "lib/esm/index.js",
7
+ "types": "lib/types/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./lib/types/index.d.ts",
11
+ "default": "./lib/esm/index.js"
12
+ }
13
+ },
14
+ "scripts": {
15
+ "clear": "rm -rf lib tsconfig.tsbuildinfo tsconfig.types.tsbuildinfo test.gjs.mjs test.node.mjs || exit 0",
16
+ "check": "tsc --noEmit",
17
+ "build": "yarn build:gjsify && yarn build:types",
18
+ "build:gjsify": "gjsify build --library 'src/**/*.{ts,js}' --exclude 'src/**/*.spec.{mts,ts}' 'src/test.{mts,ts}'",
19
+ "build:types": "tsc",
20
+ "build:test": "yarn build:test:gjs && yarn build:test:node",
21
+ "build:test:gjs": "gjsify build src/test.mts --app gjs --outfile test.gjs.mjs",
22
+ "build:test:node": "gjsify build src/test.mts --app node --outfile test.node.mjs",
23
+ "test": "yarn build:gjsify && yarn build:test && yarn test:node && yarn test:gjs",
24
+ "test:gjs": "gjs -m test.gjs.mjs",
25
+ "test:node": "node test.node.mjs"
26
+ },
27
+ "keywords": [
28
+ "gjs",
29
+ "node",
30
+ "vm"
31
+ ],
32
+ "devDependencies": {
33
+ "@gjsify/cli": "^0.1.0",
34
+ "@gjsify/unit": "^0.1.0",
35
+ "@types/node": "^25.5.0",
36
+ "typescript": "^6.0.2"
37
+ }
38
+ }