Skip to content

Commit

Permalink
fix(E.C): anonymous methods now work
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Oct 12, 2019
1 parent 29d435b commit c0e11a8
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 69 deletions.
118 changes: 52 additions & 66 deletions src/E.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,55 +50,55 @@ export default function makeE(HandledPromise) {
return harden(new Proxy({}, handler));
}

let EChain;
const makers = {
G(x) {
// Return getter.
return new Proxy(
{},
{
has(_target, _prop) {
return true;
},
get(_target, prop) {
return EChain(HandledPromise.get(x, prop));
},
},
);
},
D(x) {
// Return deleter.
return new Proxy(
{},
{
has(_target, _prop) {
return true;
},
get(_target, prop) {
return EChain(HandledPromise.delete(x, prop));
const EChain = x => {
return harden({
get G() {
// Return getter.
return new Proxy(
{ EChain: 'getter' },
{
has(_target, _prop) {
return true;
},
get(_target, prop) {
return EChain(HandledPromise.get(x, prop));
},
},
},
);
},
S(x) {
// Return setter.
return new Proxy(
{},
{
has(_target, _prop) {
return true;
);
},
get D() {
// Return deleter.
return new Proxy(
{ EChain: 'deleter' },
{
has(_target, _prop) {
return true;
},
get(_target, prop) {
return EChain(HandledPromise.delete(x, prop));
},
},
get(_target, prop) {
return harden(value => EChain(HandledPromise.set(x, prop, value)));
);
},
get S() {
// Return setter.
return new Proxy(
{ EChain: 'setter' },
{
has(_target, _prop) {
return true;
},
get(_target, prop) {
return harden(value =>
EChain(HandledPromise.set(x, prop, value)),
);
},
},
},
);
},
M(x) {
// Return method-caller.
return new Proxy(
{},
{
);
},
get M() {
// Return method-caller.
return new Proxy((..._args) => {}, {
has(_target, _prop) {
return true;
},
Expand All @@ -110,27 +110,13 @@ export default function makeE(HandledPromise) {
apply(_target, _thisArg, args = []) {
return EChain(HandledPromise.applyFunction(x, args));
},
},
);
},
P(x) {
// Return as promise.
return Promise.resolve(x);
},
};

EChain = x => {
return new Proxy(
{}, // empty shadow
{
has(_target, prop) {
return Object.keys(makers).indexOf(prop) >= 0;
},
get(_target, prop) {
return harden(makers[prop](x));
},
});
},
get P() {
// Return as promise.
return Promise.resolve(x);
},
);
});
};

E.C = EChain;
Expand Down
5 changes: 2 additions & 3 deletions test/test-e.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ test('E method calls', async t => {
}
});

test.only('E.C chains', async t => {
test('E.C chains', async t => {
try {
const x = {
name: 'buddy',
Expand All @@ -34,8 +34,7 @@ test.only('E.C chains', async t => {
};
const xC = E.C(x);
t.equal(await xC.M.hello('Hello').P, 'Hello, buddy!', 'method call works');
// console.log(await xC.G.y.G.fn.P);
// t.equal(await xC.G.y.G.fn.M(4).P, 8, 'anonymous method works');
t.equal(await xC.G.y.G.fn.M(4).P, 8, 'anonymous method works');
t.equal(await xC.G.val.P, 123, 'property get');
t.equal(await xC.S.val(999).P, 999, 'property set');
t.equal(x.val, 999, 'property set works');
Expand Down

0 comments on commit c0e11a8

Please sign in to comment.