forked from Agoric/agoric-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refs Agoric#3
- Loading branch information
Showing
13 changed files
with
193 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"name": "@agoric/evaluate", | ||
"version": "0.0.1", | ||
"description": "(unsafe) two-argument evaluator funcion", | ||
"main": "src/index.js", | ||
"module": "src/main.js", | ||
"engines": { | ||
"node": ">=10.15.1" | ||
}, | ||
"scripts": { | ||
"test": "tape -r esm test/**/test*.js" | ||
}, | ||
"devDependencies": { | ||
"esm": "^3.2.5", | ||
"tape": "^4.10.0" | ||
}, | ||
"dependencies": {}, | ||
"author": "Agoric", | ||
"license": "Apache-2.0", | ||
"bugs": { | ||
"url": "https://github.com/Agoric/DomainVat/issues" | ||
}, | ||
"homepage": "https://github.com/Agoric/DomainVat#readme" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
function evaluate(source, endowments = {}) { | ||
const names = Object.getOwnPropertyNames(endowments); | ||
const s = `(function(endowments) { | ||
const { ${names.join(',')} } = endowments; | ||
return ${source}; | ||
})`; | ||
// eslint-disable-next-line no-eval | ||
return eval(s)(endowments); | ||
} | ||
|
||
module.exports = evaluate; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import evaluate from './index'; | ||
|
||
export default evaluate; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// eslint doesn't realize that this file | ||
// (agoric-evaluate/test/test-evaluate.js) is a test, since it lives in a | ||
// non-"test" subdirectory. | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
import { test } from 'tape'; | ||
import evaluate from '../src/main'; | ||
|
||
test('basic', t => { | ||
t.deepEqual(evaluate('1+2'), 3); | ||
t.deepEqual(evaluate('(a,b) => a+b')(1, 2), 3); | ||
t.deepEqual(evaluate('(function(a,b) { return a+b; })')(1, 2), 3); | ||
t.end(); | ||
}); | ||
|
||
test('endowments', t => { | ||
t.deepEqual(evaluate('1+a', { a: 2 }), 3); | ||
t.deepEqual(evaluate('(a,b) => a+b+c', { c: 3 })(1, 2), 6); | ||
t.deepEqual(evaluate('(function(a,b) { return a+b+c; })', { c: 3 })(1, 2), 6); | ||
t.deepEqual(evaluate('1+a+b', { a: 2, b: 3 }), 6); | ||
t.end(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { test } from 'tape'; | ||
import evaluate from '@agoric/evaluate'; | ||
|
||
test('basic', t => { | ||
t.deepEqual(evaluate('1+2'), 3); | ||
t.deepEqual(evaluate('(a,b) => a+b')(1, 2), 3); | ||
t.deepEqual(evaluate('(function(a,b) { return a+b; })')(1, 2), 3); | ||
t.end(); | ||
}); | ||
|
||
test('endowments', t => { | ||
t.deepEqual(evaluate('1+a', { a: 2 }), 3); | ||
t.deepEqual(evaluate('(a,b) => a+b+c', { c: 3 })(1, 2), 6); | ||
t.deepEqual(evaluate('(function(a,b) { return a+b+c; })', { c: 3 })(1, 2), 6); | ||
t.deepEqual(evaluate('1+a+b', { a: 2, b: 3 }), 6); | ||
t.end(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { test } from 'tape-promise/tape'; | ||
import { buildVatController } from '../src/index'; | ||
|
||
async function requireNat(t, withSES) { | ||
const config = { bootstrapIndexJS: require.resolve('./vat-imports-1.js') }; | ||
const c = await buildVatController(config, withSES, ['nat']); | ||
await c.step(); | ||
t.deepEqual(c.dump().log, ['nat-1', '2']); | ||
} | ||
|
||
test('vat can require nat with SES', async t => { | ||
await requireNat(t, true); | ||
t.end(); | ||
}); | ||
|
||
test('vat can require nat without SES', async t => { | ||
await requireNat(t, false); | ||
t.end(); | ||
}); | ||
|
||
async function requireHarden(t, withSES) { | ||
const config = { bootstrapIndexJS: require.resolve('./vat-imports-1.js') }; | ||
const c = await buildVatController(config, withSES, ['harden']); | ||
await c.step(); | ||
t.deepEqual(c.dump().log, ['harden-1', 'true', 'true']); | ||
} | ||
|
||
test('vat can require harden with SES', async t => { | ||
await requireHarden(t, true); | ||
t.end(); | ||
}); | ||
|
||
test('vat can require harden without SES', async t => { | ||
await requireHarden(t, false); | ||
t.end(); | ||
}); | ||
|
||
async function requireEvaluate(t, withSES) { | ||
const config = { bootstrapIndexJS: require.resolve('./vat-imports-1.js') }; | ||
const c = await buildVatController(config, withSES, ['evaluate']); | ||
await c.step(); | ||
t.deepEqual(c.dump().log, ['evaluate-1', '3', '4', '5']); | ||
} | ||
|
||
test('vat can require evaluate with SES', async t => { | ||
await requireEvaluate(t, true); | ||
t.end(); | ||
}); | ||
|
||
test('vat can require evaluate without SES', async t => { | ||
await requireEvaluate(t, false); | ||
t.end(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import harden from '@agoric/harden'; | ||
|
||
export default function setup(syscall, helpers) { | ||
const { log, makeLiveSlots } = helpers; | ||
const { dispatch, registerRoot } = makeLiveSlots(syscall, helpers.vatID); | ||
const obj0 = { | ||
bootstrap(argv, _vats) { | ||
if (argv[0] === 'nat') { | ||
log('nat-1'); | ||
// eslint-disable-next-line global-require | ||
const nat = require('@agoric/nat'); | ||
log(nat(2)); | ||
} else if (argv[0] === 'harden') { | ||
log('harden-1'); | ||
// eslint-disable-next-line global-require | ||
const harden2 = require('@agoric/harden'); | ||
const o1 = { o2: {} }; | ||
harden2(o1); | ||
log(Object.isFrozen(o1)); | ||
log(Object.isFrozen(o1.o2)); | ||
} else if (argv[0] === 'evaluate') { | ||
log('evaluate-1'); | ||
// eslint-disable-next-line global-require | ||
const evaluate = require('@agoric/evaluate'); | ||
log(evaluate('1+2')); | ||
log(evaluate('a+b', { a: 0, b: 4 })); | ||
log(evaluate('(a) => a+b', { b: 3 })(2)); | ||
} | ||
}, | ||
}; | ||
|
||
registerRoot(harden(obj0)); | ||
return dispatch; | ||
} |