git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: a12fcbf698618cd91b74a1da931067ede4b57f45

Files: a12fcbf698618cd91b74a1da931067ede4b57f45 / codeHandler.js

860 bytesRaw
1const Wasm = require('primea-wasm-container')
2
3const defaultHandler = {
4 test: (state) => {
5 return !state.code
6 },
7 init: () => {
8 return require('./defaultAgent.js')
9 }
10}
11
12const wasm = {
13 test: (state) => {
14 const code = new Buffer(state.code)
15 return code && code.slice(0, 4).toString() === '\x00asm'
16 },
17 init: (code) => {
18 return new Wasm(code)
19 }
20}
21
22const javascript = {
23 test: (state) => {
24 return typeof state.code === 'function'
25 },
26 init: (state) => {
27 return {
28 run: state.code
29 }
30 }
31}
32
33let codeHandlers = exports.handlers = {
34 default: defaultHandler,
35 wasm: wasm,
36 javascript: javascript
37}
38
39exports.init = (code) => {
40 for (let name in codeHandlers) {
41 try {
42 const handler = codeHandlers[name]
43 if (handler.test(code)) {
44 return handler.init(code)
45 }
46 } catch (e) {}
47 }
48}
49

Built with git-ssb-web