godprotocol 1.1.32 → 1.2.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.
- package/Datatypes/arr.js +559 -254
- package/Datatypes/bool.js +8 -5
- package/Datatypes/callable.js +1 -1
- package/Datatypes/cls.js +193 -24
- package/Datatypes/conf.js +2 -2
- package/Datatypes/err.js +25 -0
- package/Datatypes/func.js +89 -13
- package/Datatypes/functions/storage.js +572 -12
- package/Datatypes/functions/wallet.js +49 -0
- package/Datatypes/instance.js +87 -122
- package/Datatypes/num.js +192 -153
- package/Datatypes/str.js +188 -122
- package/Datatypes/twa.js +346 -71
- package/Datatypes/voi.js +9 -4
- package/Index.js +1 -1
- package/Instances/account.js +0 -0
- package/Instances/event.js +0 -0
- package/Instances/fold.js +103 -0
- package/Instances/folder_queries.js +46 -0
- package/Instances/response.js +31 -0
- package/Objects/Account.js +160 -110
- package/Objects/Blockweb.js +6 -11
- package/Objects/Callable.js +223 -188
- package/Objects/Chain.js +127 -142
- package/Objects/Datatypes.js +114 -588
- package/Objects/Manager.js +349 -39
- package/Objects/Oracle.js +283 -210
- package/Objects/Repository.js +12 -33
- package/Objects/Trinity.js +9 -7
- package/Objects/Utils.js +8 -8
- package/Objects/Virtual_machine.js +320 -320
- package/README.md +0 -0
- package/callables/functions/assert.js +10 -0
- package/callables/functions/display.js +2 -3
- package/callables/functions/env.js +5 -0
- package/callables/functions/event.js +14 -0
- package/callables/functions/exit.js +3 -2
- package/callables/functions/folder.js +15 -0
- package/callables/functions/get_uid.js +6 -0
- package/callables/functions/hash.js +7 -0
- package/callables/functions/import_.js +62 -0
- package/callables/functions/local.js +9 -0
- package/callables/functions/net.js +33 -18
- package/callables/functions/parse.js +35 -0
- package/callables/functions/query.js +12 -0
- package/callables/functions/render.js +9 -13
- package/callables/functions/servers.js +10 -0
- package/callables/functions/spawn.js +7 -0
- package/callables/functions/store.js +75 -0
- package/callables/functions/super_.js +24 -0
- package/callables/functions/typeof.js +32 -0
- package/callables/functions/wait.js +9 -0
- package/callables/register.js +193 -8
- package/html/create.js +110 -0
- package/html/index.js +114 -0
- package/html/load.js +165 -0
- package/html/parse.js +171 -0
- package/html/run.js +151 -0
- package/html/util.js +5 -0
- package/package.json +5 -3
- package/repos/fs.js +114 -0
- package/repos/github.js +73 -0
- package/repos/ram.js +47 -0
- package/utils/create_server.js +66 -26
- package/utils/hash.js +3 -2
- package/utils/ops.js +2 -0
- package/utils/oracle_handlers.js +7 -0
- package/utils/services.js +18 -51
- package/utils/socket_server.js +36 -0
- package/utils/vm_utils.js +241 -137
- package/callables/functions/assign.js +0 -6
package/Objects/Manager.js
CHANGED
|
@@ -1,59 +1,369 @@
|
|
|
1
|
-
import create_server from "../utils/create_server";
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import Repository from "./Repository";
|
|
1
|
+
import create_server from "../utils/create_server.js";
|
|
2
|
+
import Account from "./Account.js";
|
|
3
|
+
import GDS from "generalised-datastore";
|
|
4
|
+
import { hash } from "godprotocol/utils/hash.js";
|
|
5
|
+
import { copy_object } from "generalised-datastore/utils/functions.js";
|
|
7
6
|
|
|
8
7
|
class Manager {
|
|
9
|
-
constructor(name, options)
|
|
10
|
-
this.name = name
|
|
11
|
-
this.options = options
|
|
12
|
-
|
|
13
|
-
this.compiler = options.compiler;
|
|
8
|
+
constructor(name, options){
|
|
9
|
+
this.name = name
|
|
10
|
+
this.options = options
|
|
14
11
|
this.trinity = options.trinity;
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
12
|
+
|
|
13
|
+
this.accounts = new Object()
|
|
14
|
+
|
|
15
|
+
this.clock_speed = 0
|
|
16
|
+
this.accounts_pairs = new Object()
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
swap_result = async (id, thread)=>{
|
|
20
|
+
if (typeof id === 'number'){
|
|
21
|
+
id = thread.results[id]
|
|
18
22
|
}
|
|
19
|
-
|
|
20
|
-
|
|
23
|
+
|
|
24
|
+
return id
|
|
25
|
+
}
|
|
21
26
|
|
|
22
|
-
|
|
23
|
-
|
|
27
|
+
set_bullet = async (bullet, thread, )=>{
|
|
28
|
+
switch(bullet.type){
|
|
29
|
+
case 'assignment':
|
|
30
|
+
bullet.value = await this.swap_result(bullet.value, thread, )
|
|
31
|
+
break;
|
|
32
|
+
case 'call':
|
|
33
|
+
bullet.identifier = await this.swap_result(bullet.identifier, thread, )
|
|
34
|
+
|
|
35
|
+
for (let a = 0; a< bullet.arguments.length; a++){
|
|
36
|
+
bullet.arguments[a] = await this.swap_result(bullet.arguments[a], thread, )
|
|
37
|
+
}
|
|
38
|
+
break;
|
|
39
|
+
case 'return':
|
|
40
|
+
bullet.output = await this.swap_result(bullet.output, thread)
|
|
41
|
+
break;
|
|
42
|
+
case 'module':
|
|
43
|
+
for(let b=0; b< bullet.body.length; b++){
|
|
44
|
+
bullet.body[b] = await this.swap_result(bullet.body[b], thread)
|
|
45
|
+
}
|
|
46
|
+
break
|
|
47
|
+
case 'nest':
|
|
48
|
+
for (let n=0; n< bullet.nests.length; n++){
|
|
49
|
+
bullet.nests[n] = await this.swap_result(bullet.nests[n], thread, )
|
|
50
|
+
}
|
|
51
|
+
if (bullet.assignment){
|
|
52
|
+
bullet.assignment = await this.swap_result(bullet.assignment, thread, )
|
|
53
|
+
}
|
|
54
|
+
break;
|
|
55
|
+
case 'twain':
|
|
56
|
+
for (let key in bullet.value){
|
|
57
|
+
let [prop, value] = bullet.value[key]
|
|
58
|
+
bullet.value[key][0] = await this.swap_result(prop, thread, )
|
|
59
|
+
bullet.value[key][1] = await this.swap_result(value, thread, )
|
|
60
|
+
}
|
|
61
|
+
break
|
|
62
|
+
case 'array':
|
|
63
|
+
for (let i=0; i< bullet.value.length; i++){
|
|
64
|
+
let val = bullet.value[i]
|
|
65
|
+
bullet.value[i] = await this.swap_result(val, thread, )
|
|
66
|
+
}
|
|
67
|
+
break
|
|
68
|
+
case 'variable':
|
|
69
|
+
break;
|
|
70
|
+
case 'try':
|
|
71
|
+
}
|
|
24
72
|
|
|
25
|
-
|
|
73
|
+
return bullet
|
|
26
74
|
}
|
|
27
75
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
76
|
+
_stack = new Array()
|
|
77
|
+
|
|
78
|
+
break_count = 0
|
|
79
|
+
|
|
80
|
+
start_clock = async()=>{
|
|
81
|
+
this.clock = setInterval(async() => {
|
|
82
|
+
this.clock_running = true;
|
|
83
|
+
for (let account in this.accounts_pairs){
|
|
84
|
+
let account_pair = this.accounts_pairs[account]
|
|
85
|
+
let {threads, threadlist, state, current_thread} = account_pair
|
|
86
|
+
|
|
87
|
+
if (state === 'running')
|
|
88
|
+
continue;
|
|
89
|
+
|
|
90
|
+
let total_loop = 0
|
|
91
|
+
while(threadlist.length && (total_loop < threadlist.length)){
|
|
92
|
+
let thread_id = threadlist[account_pair.current_thread]
|
|
93
|
+
let thread = threads[thread_id]
|
|
94
|
+
if (!thread){
|
|
95
|
+
account_pair.current_thread = 0
|
|
96
|
+
continue
|
|
97
|
+
}
|
|
98
|
+
let {bullets, pointer, callback, results, chain, spawn, status, _stack, on_pointer} = thread
|
|
99
|
+
|
|
100
|
+
if (status && status.start){
|
|
101
|
+
if (Date.now() - status.start > status.duration){
|
|
102
|
+
thread.status = 'active'
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
if (status === 'active'){
|
|
106
|
+
account_pair.current_thread++
|
|
107
|
+
account_pair.state = 'running'
|
|
108
|
+
} else {
|
|
109
|
+
total_loop++
|
|
110
|
+
account_pair.current_thread++
|
|
111
|
+
continue
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
total_loop = 0
|
|
115
|
+
let current_bullet = bullets[pointer], res;
|
|
116
|
+
|
|
117
|
+
if (current_bullet){
|
|
118
|
+
if (current_bullet.type === 'loop'){
|
|
119
|
+
_stack.push(current_bullet)
|
|
120
|
+
} else if (current_bullet.type === 'try'){
|
|
121
|
+
_stack.push(current_bullet)
|
|
122
|
+
} else if(current_bullet.type === 'catch'){
|
|
123
|
+
let try_conf = _stack.pop()
|
|
124
|
+
thread.pointer += try_conf.catch_offset
|
|
125
|
+
} else if(current_bullet.type === 'endloop'){
|
|
126
|
+
let loop_conf = _stack.pop()
|
|
127
|
+
if (!loop_conf || loop_conf.type !== 'loop'){
|
|
128
|
+
console.log(`ErrorLog: Corrupt stacking`)
|
|
129
|
+
thread.pointer = bullets.length
|
|
130
|
+
}else thread.pointer = loop_conf.conditional_jmp
|
|
131
|
+
} else {
|
|
132
|
+
let account_obj = this.accounts[account]
|
|
133
|
+
|
|
134
|
+
if (Object.keys(current_bullet).includes('jmp')){}
|
|
135
|
+
else
|
|
136
|
+
res = await account_obj.vm.execute(this.set_bullet(copy_object(current_bullet), thread), {chain, manager: this, thread})
|
|
137
|
+
|
|
138
|
+
if (res && res.__interrupt__){
|
|
139
|
+
if (res.type === 'return'){
|
|
140
|
+
if (spawn){
|
|
141
|
+
let thrd = threads[spawn.thread_id]
|
|
142
|
+
thrd.results[spawn.pointer] = res.output
|
|
143
|
+
thread.pointer = bullets.length-1
|
|
144
|
+
}else res = res.output
|
|
145
|
+
} else if (['break', 'continue'].includes(res.__interrupt__)){
|
|
146
|
+
let caught;
|
|
147
|
+
while(_stack.length){
|
|
148
|
+
let loop = _stack.slice(-1)[0]
|
|
149
|
+
|
|
150
|
+
if (loop && loop.type === 'loop'){
|
|
151
|
+
if (res.__interrupt__ === 'break'){
|
|
152
|
+
thread.pointer = loop.conditional_jmp -1
|
|
153
|
+
}else if(res.__interrupt__ === 'continue'){
|
|
154
|
+
thread.pointer = loop.update_jmp -1
|
|
155
|
+
}
|
|
156
|
+
caught = true
|
|
157
|
+
break
|
|
158
|
+
}
|
|
159
|
+
_stack.pop()
|
|
160
|
+
}
|
|
161
|
+
if (!caught){
|
|
162
|
+
console.log(`ErrorLog: Loop keywords can only be used inside a loop`)
|
|
163
|
+
thread.pointer = bullets.length
|
|
164
|
+
}
|
|
165
|
+
}else {
|
|
166
|
+
let i = _stack.length - 1, caught;
|
|
167
|
+
while (_stack.length){
|
|
168
|
+
let tstack = _stack[i]
|
|
169
|
+
i--
|
|
170
|
+
let t_catch = bullets[tstack.catch_index]
|
|
171
|
+
|
|
172
|
+
if (!t_catch.objects.length || t_catch.objects.includes(res.payload.type)){
|
|
173
|
+
thread.pointer = tstack.catch_index
|
|
174
|
+
|
|
175
|
+
if (tstack.alias)
|
|
176
|
+
await account_obj.vm.error_instance({location: tstack.location, payload: res.payload}, tstack.alias)
|
|
177
|
+
|
|
178
|
+
_stack.pop()
|
|
179
|
+
caught = true
|
|
180
|
+
break
|
|
181
|
+
}
|
|
182
|
+
_stack.pop()
|
|
183
|
+
}
|
|
184
|
+
if (!caught){
|
|
185
|
+
console.log(`ErrorLog: ${res.payload.message}`)
|
|
186
|
+
if (spawn){
|
|
187
|
+
let thrd = threads[spawn.thread_id]
|
|
188
|
+
thrd.results[spawn.pointer] = res
|
|
189
|
+
}
|
|
190
|
+
thread.pointer = bullets.length
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}else {
|
|
194
|
+
if (current_bullet.type === 'assignment'){
|
|
195
|
+
bullets.map(bull=>{
|
|
196
|
+
if (bull.type === 'variable' && bull.location === current_bullet.location){
|
|
197
|
+
bull._id = res._id
|
|
198
|
+
}
|
|
199
|
+
})
|
|
200
|
+
}
|
|
201
|
+
if (current_bullet.type === 'nest' && current_bullet.assignment){
|
|
202
|
+
if (current_bullet.assignment_location){
|
|
203
|
+
bullets.map(bull=>{
|
|
204
|
+
if (bull.type === 'variable' && bull.location === current_bullet.assignment_location){
|
|
205
|
+
bull._id = res._id
|
|
206
|
+
let trd = thread
|
|
207
|
+
while (trd.spawn){
|
|
208
|
+
let tid = trd.spawn.thread_id
|
|
209
|
+
trd = threads[tid]
|
|
210
|
+
trd.bullets.map(bl=>{
|
|
211
|
+
if (bl.type === 'variable' && bl.location === current_bullet.assignment_location){
|
|
212
|
+
bl._id = res._id
|
|
213
|
+
}
|
|
214
|
+
})
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
})
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
if (current_bullet.conditional_jmp){
|
|
221
|
+
if (!res.literal){
|
|
222
|
+
console.log("Manager.js::SHOUT!")
|
|
223
|
+
}
|
|
224
|
+
let pass = await res.literal()
|
|
225
|
+
|
|
226
|
+
if (current_bullet.invert){
|
|
227
|
+
if(!pass){
|
|
228
|
+
thread.pointer = current_bullet.conditional_jmp - 1
|
|
229
|
+
}
|
|
230
|
+
}else{
|
|
231
|
+
if (!pass){
|
|
232
|
+
thread.pointer = current_bullet.conditional_jmp - 1
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
if (current_bullet.jmp){
|
|
237
|
+
thread.pointer = current_bullet.jmp - 1
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
results[pointer] = res
|
|
242
|
+
|
|
243
|
+
on_pointer && await on_pointer(pointer, res)
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
thread.pointer++
|
|
248
|
+
if (thread.pointer >= bullets.length){
|
|
249
|
+
callback && await callback(res, thread)
|
|
250
|
+
delete threads[thread_id]
|
|
251
|
+
delete this.accounts[account].vm.envs[thread_id]
|
|
252
|
+
account_pair.current_thread --
|
|
253
|
+
threadlist.splice(account_pair.current_thread, 1)
|
|
254
|
+
if (spawn){
|
|
255
|
+
let thrd = threads[spawn.thread_id]
|
|
256
|
+
if(thrd){
|
|
257
|
+
thrd.status = 'active'
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
if (account_pair.current_thread === threadlist.length){
|
|
262
|
+
account_pair.current_thread = 0
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
account_pair.state = 'active'
|
|
266
|
+
break
|
|
267
|
+
}
|
|
268
|
+
if (!threadlist.length){
|
|
269
|
+
delete this.accounts_pairs[account]
|
|
270
|
+
if (!Object.keys(this.accounts_pairs).length){
|
|
271
|
+
this.clock_running = false
|
|
272
|
+
clearInterval(this.clock)
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
}, this.clock_speed);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
load = async(thread, account, options={})=>{
|
|
280
|
+
let {callback, on_pointer, spawn, addr, pointer} = options
|
|
281
|
+
let tracker = this.accounts_pairs[account.name]
|
|
282
|
+
|
|
283
|
+
if (!tracker){
|
|
284
|
+
tracker = {
|
|
285
|
+
threadlist: [],
|
|
286
|
+
threads: {},
|
|
287
|
+
current_thread: 0,
|
|
288
|
+
}
|
|
289
|
+
this.accounts_pairs[account.name] = tracker
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
let thread_id = Math.random().toString()
|
|
293
|
+
tracker.threadlist.push(thread_id)
|
|
294
|
+
tracker.threads[thread_id] = {
|
|
295
|
+
bullets: thread,
|
|
296
|
+
_id: thread_id,
|
|
297
|
+
on_pointer,
|
|
298
|
+
pointer: 0,
|
|
299
|
+
results: {},
|
|
300
|
+
_stack: new Array(),
|
|
301
|
+
status: 'active',
|
|
302
|
+
addr,
|
|
303
|
+
chain: account.chain,
|
|
304
|
+
spawn: spawn? { thread_id: spawn, pointer } : null,
|
|
305
|
+
callback
|
|
306
|
+
};
|
|
307
|
+
if (spawn){
|
|
308
|
+
let spawn_thread = tracker.threads[spawn]
|
|
309
|
+
|
|
310
|
+
spawn_thread.status = thread_id
|
|
31
311
|
}
|
|
32
|
-
this.server_object =
|
|
33
|
-
this.server && (await create_server({ ...this.server, manager: this }));
|
|
34
312
|
|
|
35
|
-
this.
|
|
313
|
+
if (!this.clock_running){
|
|
314
|
+
await this.start_clock()
|
|
315
|
+
}
|
|
36
316
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
};
|
|
317
|
+
return thread_id
|
|
318
|
+
}
|
|
40
319
|
|
|
41
|
-
|
|
42
|
-
|
|
320
|
+
sync = async()=>{
|
|
321
|
+
this.path = `${this.trinity.game}/${this.name}`
|
|
322
|
+
this.hash = hash
|
|
43
323
|
|
|
44
|
-
|
|
324
|
+
this.ds = new GDS(this)
|
|
325
|
+
await this.ds.sync()
|
|
326
|
+
if (this.options.server){
|
|
327
|
+
this.server = this.options.server
|
|
328
|
+
if (this.options.server.port)
|
|
329
|
+
this.options.server_domain = `${this.options.server.hostname}:${this.options.server.port}`
|
|
330
|
+
else this.options.server_domain = this.options.hostname
|
|
331
|
+
await create_server({manager: this, ...this.options.server, app: this.options.app })
|
|
332
|
+
}
|
|
333
|
+
if (this.options.oracle){
|
|
334
|
+
this.oracle = this.options.oracle;
|
|
335
|
+
this.oracle.set_manager(this)
|
|
336
|
+
}
|
|
337
|
+
}
|
|
45
338
|
|
|
46
|
-
|
|
47
|
-
|
|
339
|
+
get_account = async(name) => {
|
|
340
|
+
let acc = this.accounts[name]
|
|
341
|
+
if (!acc){
|
|
342
|
+
let servers = await this.oracle.get_servers(`Accounts/${name}`)
|
|
343
|
+
if(servers.length){
|
|
344
|
+
acc = await this.add_account(name, {servers})
|
|
345
|
+
}
|
|
346
|
+
}
|
|
48
347
|
|
|
49
|
-
|
|
348
|
+
return acc;
|
|
349
|
+
}
|
|
50
350
|
|
|
51
|
-
|
|
52
|
-
|
|
351
|
+
add_account = async(name, options={}) => {
|
|
352
|
+
let acc = this.accounts[name]
|
|
353
|
+
if (!acc) {
|
|
354
|
+
acc = new Account(name, {...options, manager: this})
|
|
355
|
+
let res = await acc.sync()
|
|
356
|
+
if (res)
|
|
357
|
+
this.accounts[name] = acc
|
|
358
|
+
else return
|
|
359
|
+
}
|
|
53
360
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
361
|
+
if (options.init){
|
|
362
|
+
this.init_account = acc
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
return acc;
|
|
366
|
+
}
|
|
57
367
|
}
|
|
58
368
|
|
|
59
369
|
export default Manager;
|