scriptable 0.2.1 → 0.2.3

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/dist/evaluate.js +18 -5
  2. package/package.json +1 -1
package/dist/evaluate.js CHANGED
@@ -15,6 +15,15 @@ function assertSafeProperty(prop) {
15
15
  throw new Error(`Access to property '${prop}' is not allowed`);
16
16
  }
17
17
  }
18
+ function toPropertyKey(value) {
19
+ if (value !== null &&
20
+ (typeof value === "object" ||
21
+ typeof value === "function" ||
22
+ typeof value === "symbol")) {
23
+ throw new Error("Property key must be a primitive value");
24
+ }
25
+ return String(value);
26
+ }
18
27
  // Sentinel thrown by ReturnStatement and caught at the nearest function
19
28
  // boundary (makeFunc). Without this, a `return` nested inside an `if` (or
20
29
  // any non-ReturnStatement node) would be evaluated but the enclosing
@@ -45,7 +54,7 @@ function evalAsPath(exp, env, functions) {
45
54
  if (exp.type === "MemberExpression") {
46
55
  const objParts = evalAsPath(exp.object, env, functions);
47
56
  const propStr = exp.computed
48
- ? String(evaluate(exp.property, env, functions))
57
+ ? toPropertyKey(evaluate(exp.property, env, functions))
49
58
  : String(exp.property.name);
50
59
  assertSafeProperty(propStr);
51
60
  return [...objParts, propStr];
@@ -77,6 +86,10 @@ const evals = {
77
86
  const params = exp.params.map((param) => evaluate(param));
78
87
  return makeFunc(params, exp.body, env, functions);
79
88
  },
89
+ FunctionExpression(exp, env, functions) {
90
+ const params = exp.params.map((param) => evaluate(param));
91
+ return makeFunc(params, exp.body, env, functions);
92
+ },
80
93
  ArrayExpression(exp, env, functions) {
81
94
  const result = [];
82
95
  for (const el of exp.elements) {
@@ -238,9 +251,9 @@ const evals = {
238
251
  MemberExpression(exp, env, functions) {
239
252
  const obj = evaluate(exp.object, env, functions);
240
253
  if (exp.computed) {
241
- const prop = evaluate(exp.property, env, functions);
242
- assertSafeProperty(String(prop));
243
- return obj[prop];
254
+ const key = toPropertyKey(evaluate(exp.property, env, functions));
255
+ assertSafeProperty(key);
256
+ return obj[key];
244
257
  }
245
258
  const prop = evaluate(exp.property);
246
259
  assertSafeProperty(prop);
@@ -281,7 +294,7 @@ const evals = {
281
294
  // silently used the identifier NAME "k" instead of its value.
282
295
  let key;
283
296
  if (prop.computed) {
284
- key = String(evaluate(prop.key, env, functions));
297
+ key = toPropertyKey(evaluate(prop.key, env, functions));
285
298
  }
286
299
  else if (prop.key.type === "Identifier") {
287
300
  key = prop.key.name;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "scriptable",
3
- "version": "0.2.1",
3
+ "version": "0.2.3",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",