scriptable 0.0.6 → 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.
Files changed (40) hide show
  1. package/.claude/settings.local.json +10 -0
  2. package/README.md +139 -0
  3. package/dist/binary-ops.d.ts +2 -0
  4. package/dist/binary-ops.js +16 -0
  5. package/dist/builtin-functions.d.ts +3 -0
  6. package/dist/builtin-functions.js +151 -0
  7. package/dist/environment.d.ts +16 -0
  8. package/dist/environment.js +111 -0
  9. package/dist/evaluate.d.ts +5 -0
  10. package/dist/evaluate.js +232 -0
  11. package/dist/index.d.ts +13 -0
  12. package/dist/index.js +26 -0
  13. package/dist/logic-ops.d.ts +2 -0
  14. package/dist/logic-ops.js +5 -0
  15. package/package.json +17 -50
  16. package/src/__tests__/{break-out.js → break-out.test.ts} +3 -14
  17. package/src/__tests__/{builtin-functions-date.js → builtin-functions-date.test.ts} +30 -11
  18. package/src/__tests__/{builtin-functions.js → builtin-functions.test.ts} +2 -3
  19. package/src/__tests__/{expressions.js → expressions.test.ts} +2 -3
  20. package/src/__tests__/{functions.js → functions.test.ts} +7 -8
  21. package/src/__tests__/{logic.js → logic.test.ts} +2 -3
  22. package/src/__tests__/new-features.test.ts +158 -0
  23. package/src/__tests__/security.test.ts +399 -0
  24. package/src/__tests__/{variables.js → variables.test.ts} +3 -6
  25. package/src/binary-ops.ts +18 -0
  26. package/src/builtin-functions.ts +177 -0
  27. package/src/environment.ts +132 -0
  28. package/src/evaluate.ts +275 -0
  29. package/src/index.ts +42 -0
  30. package/src/logic-ops.ts +6 -0
  31. package/tsconfig.json +16 -0
  32. package/.npmignore +0 -1
  33. package/src/__tests__/index.js +0 -85
  34. package/src/binary-ops.js +0 -13
  35. package/src/builtin-functions.js +0 -58
  36. package/src/environment.js +0 -73
  37. package/src/evaluate.js +0 -174
  38. package/src/index.js +0 -26
  39. package/src/logic-ops.js +0 -6
  40. package/yarn.lock +0 -2632
package/src/evaluate.js DELETED
@@ -1,174 +0,0 @@
1
- // const set = require("lodash/set");
2
- const forEach = require("lodash/forEach");
3
- // const cloneDeep = require("lodash/cloneDeep");
4
-
5
- const binaryOps = require("./binary-ops");
6
- const logicOps = require("./logic-ops");
7
-
8
- const DEBUG = false;
9
-
10
- function makeFunc(params, body, env, functions) {
11
- DEBUG && console.log({ params, body });
12
- // let localEnv = cloneDeep(env);
13
- const scope = env.extend();
14
- return (...args) => {
15
- params.forEach((param, idx) => {
16
- scope.def(param, idx < args.length ? args[idx] : false);
17
- });
18
- return evaluate(body, scope, functions);
19
- };
20
- }
21
-
22
- const evals = {
23
- ArrowFunctionExpression(exp, env, functions) {
24
- const params = exp.params.map(param => evaluate(param));
25
- DEBUG && console.log({ exp, env, params: exp.params });
26
- return makeFunc(params, exp.body, env, functions);
27
- },
28
- ArrayExpression(exp, env, functions) {
29
- return exp.elements.map(el => evaluate(el, env, functions));
30
- },
31
- AssignmentExpression(exp, env, functions) {
32
- const target = evaluate(exp.left);
33
- return env.set(target, evaluate(exp.right, env, functions), exp.loc.start);
34
- },
35
- BinaryExpression(exp, env, functions) {
36
- DEBUG && console.log({ exp, env });
37
- const left = evaluate(exp.left, env, functions);
38
- const right = evaluate(exp.right, env, functions);
39
- return binaryOps[exp.operator](left, right);
40
- },
41
- BlockStatement(exp, env, functions) {
42
- DEBUG && console.log({ exp, env });
43
- const scope = env.extend();
44
- let result;
45
- forEach(exp.body, part => {
46
- result = evaluate(part, scope, functions);
47
- if (part.type === "ReturnStatement") {
48
- return false;
49
- }
50
- });
51
- return result;
52
- },
53
- CallExpression(exp, env, functions) {
54
- const func = evaluate(exp.callee);
55
- const args = exp.arguments.map(arg => evaluate(arg, env, functions));
56
- DEBUG && console.log({ exp, env, func, args });
57
- if (functions[func]) {
58
- return functions[func](...args);
59
- }
60
-
61
- if (env.defined(func)) {
62
- const customFunc = env.get(func, exp.loc.start);
63
- if (typeof customFunc === "function") {
64
- return customFunc(...args);
65
- }
66
- }
67
- const loc = exp.loc.start;
68
- throw new Error(`${func} is not a function (${loc.line}:${loc.column})`);
69
- },
70
- ExpressionStatement(exp, env, functions) {
71
- return evaluate(exp.expression, env, functions);
72
- },
73
- File(exp, env, functions) {
74
- return evaluate(exp.program, env, functions);
75
- },
76
- FunctionDeclaration(exp, env, functions) {
77
- const name = evaluate(exp.id);
78
- const params = exp.params.map(param => evaluate(param));
79
- DEBUG && console.log({ exp, env, name, params });
80
- env.set(name, makeFunc(params, exp.body, env, functions), exp.loc.start);
81
- },
82
- Identifier(exp, env, functions) {
83
- DEBUG && console.log({ exp, env });
84
- if (env) {
85
- return env.get(exp.name, exp.loc.start);
86
- }
87
- return exp.name;
88
- },
89
- IfStatement(exp, env, functions) {
90
- const test = evaluate(exp.test, env, functions);
91
- if (test) {
92
- return evaluate(exp.consequent, env, functions);
93
- }
94
- },
95
- Literal(exp) {
96
- DEBUG && console.log({ exp });
97
- return exp.value;
98
- },
99
- LogicalExpression(exp, env, functions) {
100
- DEBUG && console.log({ exp, env });
101
- const left = evaluate(exp.left, env, functions);
102
- const right = evaluate(exp.right, env, functions);
103
- return logicOps[exp.operator](left, right);
104
- },
105
- MemberExpression(exp, env, functions) {
106
- if (!env) {
107
- const obj = evaluate(exp.object);
108
- const prop = evaluate(exp.property);
109
- return `${obj}.${prop}`;
110
- }
111
- const obj = evaluate(exp.object, env, functions);
112
- if (exp.computed) {
113
- const prop = evaluate(exp.property, env, functions);
114
- return obj[prop];
115
- }
116
- return obj[evaluate(exp.property)];
117
- // const prop = evaluate(exp.property, obj, functions);
118
- // DEBUG && console.log({ exp, env, obj, prop });
119
- // if (exp.computed) {
120
- // return obj[prop];
121
- // }
122
- // return prop;
123
- },
124
- ObjectExpression(exp, env, functions) {
125
- DEBUG && console.log({ exp, env });
126
- return exp.properties.reduce((res, prop) => {
127
- res[prop.key.name] = evaluate(prop.value, env, functions);
128
- return res;
129
- }, {});
130
- },
131
- Program(exp, env, functions) {
132
- DEBUG && console.log({ exp, env });
133
- // only return the result of the last "line"
134
- let result;
135
- exp.body.forEach(exp => {
136
- result = evaluate(exp, env, functions);
137
- });
138
- return result;
139
- },
140
- ReturnStatement(exp, env, functions) {
141
- DEBUG && console.log({ exp, env });
142
- return evaluate(exp.argument, env, functions);
143
- },
144
- VariableDeclaration(exp, env, functions) {
145
- // exp.kind = var | let | const
146
- DEBUG && console.log({ exp, env });
147
- if (exp.kind === "var") {
148
- // set in global scope
149
- const globalScope = env.getGlobalScope();
150
- exp.declarations.forEach(dec => {
151
- evaluate(dec, globalScope, functions);
152
- });
153
- return;
154
- }
155
- exp.declarations.forEach(dec => {
156
- evaluate(dec, env, functions);
157
- });
158
- },
159
- VariableDeclarator(exp, env, functions) {
160
- DEBUG && console.log({ exp, env });
161
- const name = evaluate(exp.id);
162
- const value = exp.init && evaluate(exp.init);
163
- return env.def(name, value || undefined);
164
- }
165
- };
166
-
167
- function evaluate(exp, env, functions) {
168
- if (evals[exp.type]) {
169
- return evals[exp.type](exp, env, functions);
170
- }
171
- throw new Error("Cannot evaluate " + exp.type);
172
- }
173
-
174
- module.exports = evaluate;
package/src/index.js DELETED
@@ -1,26 +0,0 @@
1
- const babylon = require("babylon");
2
- const evaluate = require("./evaluate");
3
- const builtinFuncs = require("./builtin-functions");
4
- const Environment = require("./environment");
5
-
6
- function parse(code) {
7
- return babylon.parse(code, {
8
- sourceType: "script",
9
- plugins: ["estree"]
10
- });
11
- }
12
-
13
- function runner({ functions = builtinFuncs } = {}) {
14
- return (code, env = {}, debug) => {
15
- const ast = parse(code);
16
- debug && console.log(JSON.stringify(ast.program.body, null, 2));
17
- const globalEnv = new Environment();
18
- Object.keys(env).forEach(key => {
19
- globalEnv.def(key, env[key]);
20
- });
21
- const result = evaluate(ast, globalEnv, functions);
22
- return { result, env: globalEnv.vars };
23
- };
24
- }
25
-
26
- module.exports = runner;
package/src/logic-ops.js DELETED
@@ -1,6 +0,0 @@
1
- const logicOps = {
2
- "||": (l, r) => l || r,
3
- "&&": (l, r) => l && r
4
- };
5
-
6
- module.exports = logicOps;