@gjsify/vm 0.3.12 → 0.3.14

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.
Files changed (2) hide show
  1. package/lib/esm/index.js +82 -57
  2. package/package.json +3 -3
package/lib/esm/index.js CHANGED
@@ -1,68 +1,93 @@
1
- const contextSymbol = /* @__PURE__ */ Symbol("vm.context");
1
+ //#region src/index.ts
2
+ const contextSymbol = Symbol("vm.context");
3
+ /**
4
+ * Run code in the current V8/SpiderMonkey context.
5
+ * Equivalent to eval() but matches Node.js vm.runInThisContext() API.
6
+ */
2
7
  function runInThisContext(code, _options) {
3
- return eval(code);
8
+ return eval(code);
4
9
  }
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);
10
+ /**
11
+ * Run code with a sandbox object providing global-like variables.
12
+ * Uses Function constructor to inject sandbox properties as local variables.
13
+ * NOTE: This is NOT a security sandbox — code can still access globalThis.
14
+ * This matches Node.js vm module behavior which also does not provide true isolation.
15
+ */
16
+ function runInNewContext(code, context, _options) {
17
+ const sandbox = context || {};
18
+ const keys = Object.keys(sandbox);
19
+ const values = keys.map((k) => sandbox[k]);
20
+ const fn = new Function(...keys, `return eval(${JSON.stringify(code)})`);
21
+ return fn(...values);
11
22
  }
12
- function runInContext(code2, context, _options2) {
13
- return runInNewContext(code2, context);
23
+ /**
24
+ * Run code in a previously created context.
25
+ * Since we don't have real VM contexts, this delegates to runInNewContext.
26
+ */
27
+ function runInContext(code, context, _options) {
28
+ return runInNewContext(code, context);
14
29
  }
30
+ /**
31
+ * Create a "context" object. Marks it with a symbol so isContext() works.
32
+ * In real Node.js, this creates a V8 Context. Here it just marks an object.
33
+ */
15
34
  function createContext(context) {
16
- const ctx = context || {};
17
- Object.defineProperty(ctx, contextSymbol, { value: true, enumerable: false });
18
- return ctx;
35
+ const ctx = context || {};
36
+ Object.defineProperty(ctx, contextSymbol, {
37
+ value: true,
38
+ enumerable: false
39
+ });
40
+ return ctx;
19
41
  }
42
+ /**
43
+ * Check if an object was created by createContext().
44
+ * Throws TypeError for non-object arguments, matching Node.js behavior.
45
+ */
20
46
  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;
47
+ if (typeof context !== "object" || context === null) {
48
+ throw new TypeError(`The "object" argument must be of type object. Received ${context === null ? "null" : `type ${typeof context}`}`);
49
+ }
50
+ return context[contextSymbol] === true;
27
51
  }
28
- function compileFunction(code2, params, _options2) {
29
- const paramNames = params || [];
30
- return new Function(...paramNames, code2);
52
+ /**
53
+ * Compile a function from source code with optional parameters and context.
54
+ * Matches Node.js vm.compileFunction() API surface.
55
+ * eslint-disable-next-line no-new-func — intentional: vm module implements code evaluation
56
+ */
57
+ function compileFunction(code, params, _options) {
58
+ const paramNames = params || [];
59
+ return new Function(...paramNames, code);
31
60
  }
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
61
+ /**
62
+ * Script class — compiles code for repeated execution.
63
+ */
64
+ var Script = class {
65
+ _code;
66
+ constructor(code, _options) {
67
+ this._code = code;
68
+ }
69
+ runInThisContext(_options) {
70
+ return eval(this._code);
71
+ }
72
+ runInNewContext(context, _options) {
73
+ return runInNewContext(this._code, context);
74
+ }
75
+ runInContext(context, _options) {
76
+ return runInNewContext(this._code, context);
77
+ }
78
+ createCachedData() {
79
+ return new Uint8Array(0);
80
+ }
58
81
  };
59
- export {
60
- Script,
61
- compileFunction,
62
- createContext,
63
- index_default as default,
64
- isContext,
65
- runInContext,
66
- runInNewContext,
67
- runInThisContext
82
+ var src_default = {
83
+ runInThisContext,
84
+ runInNewContext,
85
+ runInContext,
86
+ createContext,
87
+ isContext,
88
+ compileFunction,
89
+ Script
68
90
  };
91
+
92
+ //#endregion
93
+ export { Script, compileFunction, createContext, src_default as default, isContext, runInContext, runInNewContext, runInThisContext };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gjsify/vm",
3
- "version": "0.3.12",
3
+ "version": "0.3.14",
4
4
  "description": "Node.js vm module for Gjs",
5
5
  "type": "module",
6
6
  "module": "lib/esm/index.js",
@@ -30,8 +30,8 @@
30
30
  "vm"
31
31
  ],
32
32
  "devDependencies": {
33
- "@gjsify/cli": "^0.3.12",
34
- "@gjsify/unit": "^0.3.12",
33
+ "@gjsify/cli": "^0.3.14",
34
+ "@gjsify/unit": "^0.3.14",
35
35
  "@types/node": "^25.6.0",
36
36
  "typescript": "^6.0.3"
37
37
  }