motoko 0.1.5 → 2.0.2
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +153 -9
- package/contrib/hljs.js +109 -110
- package/contrib/monaco.js +2 -2
- package/index.js +6 -0
- package/interpreter.js +6 -0
- package/lib/__tests__/index.test.js +40 -0
- package/lib/__tests__/interpreter.test.js +15 -0
- package/lib/file.js +66 -0
- package/lib/index.js +112 -78
- package/lib/package.js +2 -4
- package/package.json +8 -5
- package/versions/latest/didc.min.js +1 -0
- package/versions/latest/moc.min.js +1 -0
- package/versions/latest/moc_interpreter.min.js +1 -0
- package/lib/generated/moc.js +0 -1
- package/lib/index.spec.js +0 -31
package/lib/index.js
CHANGED
@@ -1,86 +1,120 @@
|
|
1
1
|
'use strict';
|
2
2
|
|
3
|
-
const
|
3
|
+
const { file } = require('./file');
|
4
|
+
const { /* findPackage, */ loadPackages } = require('./package');
|
4
5
|
|
5
|
-
|
6
|
-
const
|
6
|
+
module.exports = (compiler, version) => {
|
7
|
+
const debug = require('debug')(version ? `motoko:${version}` : 'motoko');
|
7
8
|
|
8
|
-
const invoke = (key, unwrap, args) => {
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
try {
|
14
|
-
result = Motoko[key](...args);
|
15
|
-
} catch (err) {
|
16
|
-
if (err instanceof Error) {
|
17
|
-
throw err;
|
9
|
+
const invoke = (key, unwrap, args) => {
|
10
|
+
if (!compiler) {
|
11
|
+
throw new Error(
|
12
|
+
'Please load a Motoko compiler before running this function',
|
13
|
+
);
|
18
14
|
}
|
19
|
-
|
20
|
-
`
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
}
|
35
|
-
return result.code;
|
36
|
-
};
|
37
|
-
|
38
|
-
module.exports = {
|
39
|
-
Motoko,
|
40
|
-
// resolvePackage,
|
41
|
-
loadPackages,
|
42
|
-
addFile(file, content = '') {
|
43
|
-
if (typeof content !== 'string') {
|
44
|
-
throw new Error('Non-string file content');
|
15
|
+
if (typeof compiler[key] !== 'function') {
|
16
|
+
throw new Error(`Unknown compiler function: '${key}'`);
|
17
|
+
}
|
18
|
+
let result;
|
19
|
+
try {
|
20
|
+
result = compiler[key](...args);
|
21
|
+
} catch (err) {
|
22
|
+
if (err instanceof Error) {
|
23
|
+
throw err;
|
24
|
+
}
|
25
|
+
throw new Error(
|
26
|
+
`Unable to execute ${key}(${[...args]
|
27
|
+
.map((x) => typeof x)
|
28
|
+
.join(', ')}):\n${JSON.stringify(err)}`,
|
29
|
+
);
|
45
30
|
}
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
}
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
31
|
+
if (!unwrap) {
|
32
|
+
return result;
|
33
|
+
}
|
34
|
+
if (!result.code) {
|
35
|
+
throw new Error(
|
36
|
+
result.diagnostics
|
37
|
+
? result.diagnostics
|
38
|
+
.map(({ message }) => message)
|
39
|
+
.join('; ')
|
40
|
+
: '(no diagnostics)',
|
41
|
+
);
|
42
|
+
}
|
43
|
+
return result.code;
|
44
|
+
};
|
45
|
+
|
46
|
+
const mo = {
|
47
|
+
version,
|
48
|
+
compiler,
|
49
|
+
file(path) {
|
50
|
+
return file(mo, path);
|
51
|
+
},
|
52
|
+
// findPackage,
|
53
|
+
async loadPackages(packages) {
|
54
|
+
return loadPackages(mo, packages);
|
55
|
+
},
|
56
|
+
read(path) {
|
57
|
+
return invoke('readFile', false, [path]);
|
58
|
+
},
|
59
|
+
write(path, content = '') {
|
60
|
+
if (typeof content !== 'string') {
|
61
|
+
throw new Error('Non-string file content');
|
62
|
+
}
|
63
|
+
debug('+file', path);
|
64
|
+
invoke('saveFile', false, [path, content]);
|
65
|
+
},
|
66
|
+
rename(path, newPath) {
|
67
|
+
invoke('renameFile', false, [path, newPath]);
|
68
|
+
},
|
69
|
+
delete(path) {
|
70
|
+
debug('-file', path);
|
71
|
+
invoke('removeFile', false, [path]);
|
72
|
+
},
|
73
|
+
list(directory) {
|
74
|
+
return invoke('readDir', false, [directory]);
|
75
|
+
},
|
76
|
+
addPackage(name, directory) {
|
77
|
+
debug('+package', name, directory);
|
78
|
+
invoke('addPackage', false, [name, directory]);
|
79
|
+
},
|
80
|
+
clearPackages() {
|
81
|
+
debug('-packages');
|
82
|
+
invoke('clearPackage', false, []);
|
83
|
+
},
|
84
|
+
setAliases(aliases) {
|
85
|
+
debug('aliases', aliases);
|
86
|
+
invoke('setActorAliases', false, [Object.entries(aliases)]);
|
87
|
+
},
|
88
|
+
setMetadata(values) {
|
89
|
+
invoke('setPublicMetadata', false, [values]);
|
90
|
+
},
|
91
|
+
check(path) {
|
92
|
+
const result = invoke('check', false, [path]);
|
93
|
+
return result.diagnostics;
|
94
|
+
},
|
95
|
+
run(path, libPaths) {
|
96
|
+
return invoke('run', false, [libPaths || [], path]);
|
97
|
+
},
|
98
|
+
candid(path) {
|
99
|
+
return invoke('candid', true, [path]);
|
100
|
+
},
|
101
|
+
wasm(path, mode) {
|
102
|
+
if (!mode) {
|
103
|
+
mode = 'ic';
|
104
|
+
} else if (mode !== 'ic' && mode !== 'wasi') {
|
105
|
+
throw new Error(`Invalid WASM format: ${mode}`);
|
106
|
+
}
|
107
|
+
return invoke('compileWasm', true, [mode, path]);
|
108
|
+
},
|
109
|
+
parseMotoko(content) {
|
110
|
+
const ast = invoke('parseMotoko', true, [content]);
|
111
|
+
return ast;
|
112
|
+
},
|
113
|
+
parseCandid(content) {
|
114
|
+
const ast = invoke('parseCandid', true, [content]);
|
115
|
+
return ast;
|
116
|
+
},
|
117
|
+
};
|
118
|
+
return mo;
|
85
119
|
};
|
86
120
|
exports.default = exports;
|
package/lib/package.js
CHANGED
@@ -136,7 +136,6 @@ function parseGithubPackage(path, name) {
|
|
136
136
|
console.warn(err);
|
137
137
|
}
|
138
138
|
|
139
|
-
console.log(result);
|
140
139
|
const { name: repoName, filepath, branch, owner } = result;
|
141
140
|
|
142
141
|
return {
|
@@ -149,15 +148,14 @@ function parseGithubPackage(path, name) {
|
|
149
148
|
}
|
150
149
|
|
151
150
|
module.exports = {
|
152
|
-
// async
|
151
|
+
// async findPackage(package) {
|
153
152
|
// if (typeof package === 'string') {
|
154
153
|
// return resolve(package);
|
155
154
|
// }
|
156
155
|
// return package;
|
157
156
|
// },
|
158
|
-
async
|
157
|
+
loadPackages: async (mo, packages) => {
|
159
158
|
for (const [name, path] of Object.entries(packages)) {
|
160
|
-
const mo = require('.');
|
161
159
|
const info = parseGithubPackage(path, name);
|
162
160
|
return fetchPackage(mo, info);
|
163
161
|
}
|
package/package.json
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
{
|
2
2
|
"name": "motoko",
|
3
|
-
"version": "0.
|
3
|
+
"version": "2.0.2",
|
4
4
|
"description": "Compile Motoko smart contracts in Node.js and the browser.",
|
5
5
|
"author": "Ryan Vandersmith (https://github.com/rvanasa)",
|
6
6
|
"license": "Apache-2.0",
|
7
|
-
"main": "
|
7
|
+
"main": "./index.js",
|
8
8
|
"scripts": {
|
9
9
|
"generate": "node utils/generate",
|
10
10
|
"test": "jest"
|
@@ -14,18 +14,21 @@
|
|
14
14
|
"isomorphic-parse-github-url": "1.0.2"
|
15
15
|
},
|
16
16
|
"devDependencies": {
|
17
|
+
"@wasmer/wasi": "^1.0.2",
|
17
18
|
"cross-env": "^7.0.3",
|
18
19
|
"jest": "^28.1.3",
|
19
|
-
"prettier": "^2.7.1"
|
20
|
-
"uglify-js": "^3.16.3"
|
20
|
+
"prettier": "^2.7.1"
|
21
21
|
},
|
22
22
|
"directories": {
|
23
23
|
"lib": "lib",
|
24
24
|
"contrib": "contrib"
|
25
25
|
},
|
26
26
|
"files": [
|
27
|
+
"index.js",
|
28
|
+
"interpreter.js",
|
27
29
|
"lib",
|
28
|
-
"contrib"
|
30
|
+
"contrib",
|
31
|
+
"versions/latest"
|
29
32
|
],
|
30
33
|
"keywords": [
|
31
34
|
"motoko",
|