@ocap/contract 1.30.3 → 1.30.4
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/esm/index.mjs +3 -8
- package/esm/parser.d.mts +12 -0
- package/esm/parser.mjs +70 -0
- package/lib/index.cjs +3 -9
- package/lib/parser.cjs +71 -0
- package/lib/parser.d.cts +12 -0
- package/package.json +4 -7
package/esm/index.mjs
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
|
+
import { parseContract } from "./parser.mjs";
|
|
1
2
|
import { isValid, toTypeInfo } from "@arcblock/did";
|
|
2
3
|
import { types } from "@ocap/mcrypto";
|
|
3
4
|
import { BN } from "@ocap/util";
|
|
4
|
-
import * as esprima from "esprima";
|
|
5
5
|
import get from "lodash/get.js";
|
|
6
6
|
import set from "lodash/set.js";
|
|
7
7
|
|
|
8
8
|
//#region src/index.ts
|
|
9
|
-
const isCallExpression = (x) => x.type === "ExpressionStatement" && x.expression && x.expression.type === "CallExpression";
|
|
10
9
|
const ZERO = new BN("0");
|
|
11
10
|
const TOKEN_RECEIVER_ROLE_TYPES = [
|
|
12
11
|
types.RoleType.ROLE_APPLICATION,
|
|
@@ -46,16 +45,12 @@ const SUPPORTED_CALLS = {
|
|
|
46
45
|
const compile = (source) => {
|
|
47
46
|
if (typeof source !== "string" || !source) throw new Error("contract source must be a non-empty string");
|
|
48
47
|
try {
|
|
49
|
-
const
|
|
50
|
-
if (body.every((x) => isCallExpression(x)) === false) throw new Error("only function call expressions are allowed");
|
|
48
|
+
const parsed = parseContract(source);
|
|
51
49
|
const result = [];
|
|
52
|
-
const expressions = body.map((x) => x.expression);
|
|
53
50
|
const allowedCalls = Object.keys(SUPPORTED_CALLS);
|
|
54
|
-
for (const
|
|
55
|
-
const name = get(expression, "callee.name");
|
|
51
|
+
for (const { name, args: actualArgs } of parsed) {
|
|
56
52
|
if (allowedCalls.includes(name) === false) throw new Error(`unrecognized call ${name}`);
|
|
57
53
|
const expectedArgs = SUPPORTED_CALLS[name].args;
|
|
58
|
-
const actualArgs = expression.arguments;
|
|
59
54
|
const expectedArgCount = expectedArgs.length;
|
|
60
55
|
const actualArgCount = actualArgs.length;
|
|
61
56
|
if (actualArgCount !== expectedArgCount) throw new Error(`invalid number of arguments for ${name}, expected ${expectedArgCount}, but found ${actualArgCount}`);
|
package/esm/parser.d.mts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
//#region src/parser.d.ts
|
|
2
|
+
interface ParsedArg {
|
|
3
|
+
type: string;
|
|
4
|
+
value?: string;
|
|
5
|
+
}
|
|
6
|
+
interface ParsedCall {
|
|
7
|
+
name: string;
|
|
8
|
+
args: ParsedArg[];
|
|
9
|
+
}
|
|
10
|
+
declare const parseContract: (source: string) => ParsedCall[];
|
|
11
|
+
//#endregion
|
|
12
|
+
export { ParsedArg, ParsedCall, parseContract };
|
package/esm/parser.mjs
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
//#region src/parser.ts
|
|
2
|
+
const CALL_RE = /^([A-Za-z_$][A-Za-z0-9_$]*)\s*\(([\s\S]*)\)$/;
|
|
3
|
+
const LITERAL_RE = /^"((?:[^"\\]|\\.)*)"$|^'((?:[^'\\]|\\.)*)'$/;
|
|
4
|
+
const IDENT_RE = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
|
|
5
|
+
const NUM_RE = /^-?\d+(?:\.\d+)?$/;
|
|
6
|
+
const splitTopLevel = (input, separators) => {
|
|
7
|
+
const result = [];
|
|
8
|
+
let current = "";
|
|
9
|
+
let quote = null;
|
|
10
|
+
for (let i = 0; i < input.length; i++) {
|
|
11
|
+
const ch = input[i];
|
|
12
|
+
if (quote) {
|
|
13
|
+
current += ch;
|
|
14
|
+
if (ch === "\\" && i + 1 < input.length) current += input[++i];
|
|
15
|
+
else if (ch === quote) quote = null;
|
|
16
|
+
} else if (ch === "\"" || ch === "'") {
|
|
17
|
+
quote = ch;
|
|
18
|
+
current += ch;
|
|
19
|
+
} else if (separators.includes(ch)) {
|
|
20
|
+
result.push(current);
|
|
21
|
+
current = "";
|
|
22
|
+
} else current += ch;
|
|
23
|
+
}
|
|
24
|
+
result.push(current);
|
|
25
|
+
return result;
|
|
26
|
+
};
|
|
27
|
+
const parseArg = (raw) => {
|
|
28
|
+
const trimmed = raw.trim();
|
|
29
|
+
if (!trimmed) return { type: "Empty" };
|
|
30
|
+
const lit = trimmed.match(LITERAL_RE);
|
|
31
|
+
if (lit) return {
|
|
32
|
+
type: "Literal",
|
|
33
|
+
value: lit[1] ?? lit[2]
|
|
34
|
+
};
|
|
35
|
+
if (NUM_RE.test(trimmed)) return {
|
|
36
|
+
type: "Literal",
|
|
37
|
+
value: trimmed
|
|
38
|
+
};
|
|
39
|
+
if (IDENT_RE.test(trimmed)) return {
|
|
40
|
+
type: "Identifier",
|
|
41
|
+
value: trimmed
|
|
42
|
+
};
|
|
43
|
+
return {
|
|
44
|
+
type: "Unknown",
|
|
45
|
+
value: trimmed
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
const parseArgs = (argsStr) => {
|
|
49
|
+
if (argsStr.trim() === "") return [];
|
|
50
|
+
return splitTopLevel(argsStr, ",").map(parseArg);
|
|
51
|
+
};
|
|
52
|
+
const parseContract = (source) => {
|
|
53
|
+
const statements = splitTopLevel(source, ";\n");
|
|
54
|
+
const calls = [];
|
|
55
|
+
for (const stmt of statements) {
|
|
56
|
+
const trimmed = stmt.trim();
|
|
57
|
+
if (!trimmed) continue;
|
|
58
|
+
const m = trimmed.match(CALL_RE);
|
|
59
|
+
if (!m) throw new Error("only function call expressions are allowed");
|
|
60
|
+
const [, name, argsStr] = m;
|
|
61
|
+
calls.push({
|
|
62
|
+
name,
|
|
63
|
+
args: parseArgs(argsStr)
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
return calls;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
//#endregion
|
|
70
|
+
export { parseContract };
|
package/lib/index.cjs
CHANGED
|
@@ -1,16 +1,14 @@
|
|
|
1
1
|
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
|
|
2
|
+
const require_parser = require('./parser.cjs');
|
|
2
3
|
let _arcblock_did = require("@arcblock/did");
|
|
3
4
|
let _ocap_mcrypto = require("@ocap/mcrypto");
|
|
4
5
|
let _ocap_util = require("@ocap/util");
|
|
5
|
-
let esprima = require("esprima");
|
|
6
|
-
esprima = require_rolldown_runtime.__toESM(esprima);
|
|
7
6
|
let lodash_get = require("lodash/get");
|
|
8
7
|
lodash_get = require_rolldown_runtime.__toESM(lodash_get);
|
|
9
8
|
let lodash_set = require("lodash/set");
|
|
10
9
|
lodash_set = require_rolldown_runtime.__toESM(lodash_set);
|
|
11
10
|
|
|
12
11
|
//#region src/index.ts
|
|
13
|
-
const isCallExpression = (x) => x.type === "ExpressionStatement" && x.expression && x.expression.type === "CallExpression";
|
|
14
12
|
const ZERO = new _ocap_util.BN("0");
|
|
15
13
|
const TOKEN_RECEIVER_ROLE_TYPES = [
|
|
16
14
|
_ocap_mcrypto.types.RoleType.ROLE_APPLICATION,
|
|
@@ -50,16 +48,12 @@ const SUPPORTED_CALLS = {
|
|
|
50
48
|
const compile = (source) => {
|
|
51
49
|
if (typeof source !== "string" || !source) throw new Error("contract source must be a non-empty string");
|
|
52
50
|
try {
|
|
53
|
-
const
|
|
54
|
-
if (body.every((x) => isCallExpression(x)) === false) throw new Error("only function call expressions are allowed");
|
|
51
|
+
const parsed = require_parser.parseContract(source);
|
|
55
52
|
const result = [];
|
|
56
|
-
const expressions = body.map((x) => x.expression);
|
|
57
53
|
const allowedCalls = Object.keys(SUPPORTED_CALLS);
|
|
58
|
-
for (const
|
|
59
|
-
const name = (0, lodash_get.default)(expression, "callee.name");
|
|
54
|
+
for (const { name, args: actualArgs } of parsed) {
|
|
60
55
|
if (allowedCalls.includes(name) === false) throw new Error(`unrecognized call ${name}`);
|
|
61
56
|
const expectedArgs = SUPPORTED_CALLS[name].args;
|
|
62
|
-
const actualArgs = expression.arguments;
|
|
63
57
|
const expectedArgCount = expectedArgs.length;
|
|
64
58
|
const actualArgCount = actualArgs.length;
|
|
65
59
|
if (actualArgCount !== expectedArgCount) throw new Error(`invalid number of arguments for ${name}, expected ${expectedArgCount}, but found ${actualArgCount}`);
|
package/lib/parser.cjs
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/parser.ts
|
|
3
|
+
const CALL_RE = /^([A-Za-z_$][A-Za-z0-9_$]*)\s*\(([\s\S]*)\)$/;
|
|
4
|
+
const LITERAL_RE = /^"((?:[^"\\]|\\.)*)"$|^'((?:[^'\\]|\\.)*)'$/;
|
|
5
|
+
const IDENT_RE = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
|
|
6
|
+
const NUM_RE = /^-?\d+(?:\.\d+)?$/;
|
|
7
|
+
const splitTopLevel = (input, separators) => {
|
|
8
|
+
const result = [];
|
|
9
|
+
let current = "";
|
|
10
|
+
let quote = null;
|
|
11
|
+
for (let i = 0; i < input.length; i++) {
|
|
12
|
+
const ch = input[i];
|
|
13
|
+
if (quote) {
|
|
14
|
+
current += ch;
|
|
15
|
+
if (ch === "\\" && i + 1 < input.length) current += input[++i];
|
|
16
|
+
else if (ch === quote) quote = null;
|
|
17
|
+
} else if (ch === "\"" || ch === "'") {
|
|
18
|
+
quote = ch;
|
|
19
|
+
current += ch;
|
|
20
|
+
} else if (separators.includes(ch)) {
|
|
21
|
+
result.push(current);
|
|
22
|
+
current = "";
|
|
23
|
+
} else current += ch;
|
|
24
|
+
}
|
|
25
|
+
result.push(current);
|
|
26
|
+
return result;
|
|
27
|
+
};
|
|
28
|
+
const parseArg = (raw) => {
|
|
29
|
+
const trimmed = raw.trim();
|
|
30
|
+
if (!trimmed) return { type: "Empty" };
|
|
31
|
+
const lit = trimmed.match(LITERAL_RE);
|
|
32
|
+
if (lit) return {
|
|
33
|
+
type: "Literal",
|
|
34
|
+
value: lit[1] ?? lit[2]
|
|
35
|
+
};
|
|
36
|
+
if (NUM_RE.test(trimmed)) return {
|
|
37
|
+
type: "Literal",
|
|
38
|
+
value: trimmed
|
|
39
|
+
};
|
|
40
|
+
if (IDENT_RE.test(trimmed)) return {
|
|
41
|
+
type: "Identifier",
|
|
42
|
+
value: trimmed
|
|
43
|
+
};
|
|
44
|
+
return {
|
|
45
|
+
type: "Unknown",
|
|
46
|
+
value: trimmed
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
const parseArgs = (argsStr) => {
|
|
50
|
+
if (argsStr.trim() === "") return [];
|
|
51
|
+
return splitTopLevel(argsStr, ",").map(parseArg);
|
|
52
|
+
};
|
|
53
|
+
const parseContract = (source) => {
|
|
54
|
+
const statements = splitTopLevel(source, ";\n");
|
|
55
|
+
const calls = [];
|
|
56
|
+
for (const stmt of statements) {
|
|
57
|
+
const trimmed = stmt.trim();
|
|
58
|
+
if (!trimmed) continue;
|
|
59
|
+
const m = trimmed.match(CALL_RE);
|
|
60
|
+
if (!m) throw new Error("only function call expressions are allowed");
|
|
61
|
+
const [, name, argsStr] = m;
|
|
62
|
+
calls.push({
|
|
63
|
+
name,
|
|
64
|
+
args: parseArgs(argsStr)
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
return calls;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
//#endregion
|
|
71
|
+
exports.parseContract = parseContract;
|
package/lib/parser.d.cts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
//#region src/parser.d.ts
|
|
2
|
+
interface ParsedArg {
|
|
3
|
+
type: string;
|
|
4
|
+
value?: string;
|
|
5
|
+
}
|
|
6
|
+
interface ParsedCall {
|
|
7
|
+
name: string;
|
|
8
|
+
args: ParsedArg[];
|
|
9
|
+
}
|
|
10
|
+
declare const parseContract: (source: string) => ParsedCall[];
|
|
11
|
+
//#endregion
|
|
12
|
+
export { ParsedArg, ParsedCall, parseContract };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ocap/contract",
|
|
3
3
|
"description": "Utility to compile/validate and run ocap contract",
|
|
4
|
-
"version": "1.30.
|
|
4
|
+
"version": "1.30.4",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "wangshijun",
|
|
7
7
|
"email": "shijun@arcblock.io",
|
|
@@ -39,15 +39,12 @@
|
|
|
39
39
|
}
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@arcblock/did": "1.30.
|
|
43
|
-
"@ocap/mcrypto": "1.30.
|
|
44
|
-
"@ocap/util": "1.30.
|
|
45
|
-
"esprima": "^4.0.1",
|
|
42
|
+
"@arcblock/did": "1.30.4",
|
|
43
|
+
"@ocap/mcrypto": "1.30.4",
|
|
44
|
+
"@ocap/util": "1.30.4",
|
|
46
45
|
"lodash": "^4.17.23"
|
|
47
46
|
},
|
|
48
47
|
"devDependencies": {
|
|
49
|
-
"@types/esprima": "^4.0.6",
|
|
50
|
-
"@types/estree": "^1.0.6",
|
|
51
48
|
"@types/lodash": "^4.17.16",
|
|
52
49
|
"remark-cli": "^10.0.1",
|
|
53
50
|
"remark-preset-github": "^4.0.4"
|