-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(exo): allow richer behaviorMethods
- Loading branch information
Showing
8 changed files
with
592 additions
and
28 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
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,79 @@ | ||
/* eslint-disable max-classes-per-file */ | ||
/* eslint-disable class-methods-use-this */ | ||
// eslint-disable-next-line import/order | ||
import { test } from './prepare-test-env-ava.js'; | ||
|
||
import { passStyleOf } from '@endo/pass-style'; | ||
// import { M, getInterfaceGuardPayload } from '@endo/patterns'; | ||
// import { defineExoClass, makeExo } from '../src/exo-makers.js'; | ||
import { M, getInterfaceGuardPayload } from '@endo/patterns'; | ||
import { makeExo, defineExoClass } from '../src/exo-makers.js'; | ||
|
||
// Based on FarSubclass1 in test-far-class-instances.js | ||
class DoublerBehaviorClass { | ||
double(x) { | ||
return x + x; | ||
} | ||
} | ||
|
||
const DoublerI = M.interface('Doubler', { | ||
double: M.call(M.lte(10)).returns(M.number()), | ||
}); | ||
|
||
const doubler = makeExo('doubler', DoublerI, DoublerBehaviorClass.prototype); | ||
|
||
test('exo doubler using js classes', t => { | ||
t.is(passStyleOf(doubler), 'remotable'); | ||
t.is(doubler.double(3), 6); | ||
t.throws(() => doubler.double('x'), { | ||
message: 'In "double" method of (doubler): arg 0: "x" - Must be <= 10', | ||
}); | ||
t.throws(() => doubler.double(), { | ||
message: | ||
'In "double" method of (doubler): Expected at least 1 arguments: []', | ||
}); | ||
t.throws(() => doubler.double(12), { | ||
message: 'In "double" method of (doubler): arg 0: 12 - Must be <= 10', | ||
}); | ||
}); | ||
|
||
// Based on FarSubclass2 in test-far-class-instances.js | ||
class DoubleAdderBehaviorClass extends DoublerBehaviorClass { | ||
doubleAddSelfCall(x) { | ||
const { | ||
state: { y }, | ||
self, | ||
} = this; | ||
return self.double(x) + y; | ||
} | ||
|
||
doubleAddSuperCall(x) { | ||
const { | ||
state: { y }, | ||
} = this; | ||
return super.double(x) + y; | ||
} | ||
} | ||
|
||
const DoubleAdderI = M.interface('DoubleAdder', { | ||
...getInterfaceGuardPayload(DoublerI).methodGuards, | ||
doubleAddSelfCall: M.call(M.number()).returns(M.number()), | ||
doubleAddSuperCall: M.call(M.number()).returns(M.number()), | ||
}); | ||
|
||
const makeDoubleAdder = defineExoClass( | ||
'doubleAdderClass', | ||
DoubleAdderI, | ||
y => ({ y }), | ||
DoubleAdderBehaviorClass.prototype, | ||
); | ||
|
||
test('exo inheritance self vs super call', t => { | ||
const da = makeDoubleAdder(5); | ||
t.is(da.doubleAddSelfCall(3), 11); | ||
t.throws(() => da.doubleAddSelfCall(12), { | ||
message: | ||
'In "double" method of (doubleAdderClass): arg 0: 12 - Must be <= 10', | ||
}); | ||
t.is(da.doubleAddSuperCall(12), 29); | ||
}); |
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,177 @@ | ||
/** | ||
* Based on the WobblyPoint inheritance examples in | ||
* https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/google-caja/caja-spec-2007-12-21.pdf | ||
* and | ||
* test-far-wobbly-point.js | ||
*/ | ||
|
||
/* eslint-disable class-methods-use-this */ | ||
/* eslint-disable max-classes-per-file */ | ||
/* eslint-disable-next-line import/order */ | ||
import { test } from './prepare-test-env-ava.js'; | ||
|
||
// TODO enable import of getMethodNames without deep import | ||
// eslint-disable-next-line import/order | ||
import { getMethodNames } from '@endo/eventual-send/src/local.js'; | ||
import { passStyleOf, Far } from '@endo/pass-style'; | ||
import { M } from '@endo/patterns'; | ||
import { defineExoClass } from '../src/exo-makers.js'; | ||
import { GET_INTERFACE_GUARD } from '../src/exo-tools.js'; | ||
|
||
const { Fail, quote: q } = assert; | ||
const { apply } = Reflect; | ||
|
||
class ExoBaseClass { | ||
constructor() { | ||
Fail`Turn Exo JS classes into Exo classes with defineExoClassFromJSClass: ${q( | ||
new.target.name, | ||
)}`; | ||
} | ||
} | ||
|
||
const ExoPointI = M.interface('ExoPoint', { | ||
toString: M.call().returns(M.string()), | ||
getX: M.call().returns(M.gte(0)), | ||
getY: M.call().returns(M.number()), | ||
setY: M.call(M.number()).returns(), | ||
}); | ||
|
||
class ExoPoint extends ExoBaseClass { | ||
static implements = ExoPointI; | ||
|
||
static init(x, y) { | ||
// Heap exos currently use the returned record directly, so | ||
// needs to not be frozen for `state.y` to be assignable. | ||
// TODO not true for other zones. May make heap zone more like | ||
// the others in treatment of `state`. | ||
return { x, y }; | ||
} | ||
|
||
toString() { | ||
const { self } = this; | ||
return `<${self.getX()},${self.getY()}>`; | ||
} | ||
|
||
getX() { | ||
const { | ||
state: { x }, | ||
} = this; | ||
return x; | ||
} | ||
|
||
getY() { | ||
const { | ||
state: { y }, | ||
} = this; | ||
return y; | ||
} | ||
|
||
setY(newY) { | ||
const { state } = this; | ||
state.y = newY; | ||
} | ||
} | ||
harden(ExoPoint); | ||
|
||
const defineExoClassFromJSClass = klass => | ||
defineExoClass(klass.name, klass.implements, klass.init, klass.prototype); | ||
harden(defineExoClassFromJSClass); | ||
|
||
const makeExoPoint = defineExoClassFromJSClass(ExoPoint); | ||
|
||
test('ExoPoint instances', t => { | ||
const pt = makeExoPoint(3, 5); | ||
t.is(passStyleOf(pt), 'remotable'); | ||
t.false(pt instanceof ExoPoint); | ||
t.deepEqual(getMethodNames(pt), [ | ||
GET_INTERFACE_GUARD, | ||
'getX', | ||
'getY', | ||
'setY', | ||
'toString', | ||
]); | ||
t.is(pt.getX(), 3); | ||
t.is(pt.getY(), 5); | ||
t.is(`${pt}`, '<3,5>'); | ||
pt.setY(6); | ||
t.is(`${pt}`, '<3,6>'); | ||
|
||
const otherPt = makeExoPoint(1, 2); | ||
t.is(apply(pt.getX, otherPt, []), 1); | ||
|
||
const bigPt = makeExoPoint(-3, 5); | ||
t.throws(() => bigPt.getX(), { | ||
message: 'In "getX" method of (ExoPoint): result: -3 - Must be >= 0', | ||
}); | ||
// `self` calls are guarded | ||
t.throws(() => `${bigPt}`, { | ||
message: 'In "getX" method of (ExoPoint): result: -3 - Must be >= 0', | ||
}); | ||
}); | ||
|
||
class ExoWobblyPoint extends ExoPoint { | ||
static init(x, y, getWobble) { | ||
return { ...ExoPoint.init(x, y), getWobble }; | ||
} | ||
|
||
getX() { | ||
const { | ||
state: { getWobble }, | ||
} = this; | ||
return super.getX() + getWobble(); | ||
} | ||
} | ||
harden(ExoWobblyPoint); | ||
|
||
const makeExoWobblyPoint = defineExoClassFromJSClass(ExoWobblyPoint); | ||
|
||
test('FarWobblyPoint inheritance', t => { | ||
let wobble = 0; | ||
// For heap classes currently, there is no reason to make `getWobble` passable. | ||
// But other zones insist on at least passability, and TODO we may eventually | ||
// make the heap zone act like this as well. | ||
const getWobble = Far('getW', () => (wobble += 1)); | ||
const wpt = makeExoWobblyPoint(3, 5, getWobble); | ||
t.false(wpt instanceof ExoWobblyPoint); | ||
t.false(wpt instanceof ExoPoint); | ||
t.is(passStyleOf(wpt), 'remotable'); | ||
t.deepEqual(getMethodNames(wpt), [ | ||
GET_INTERFACE_GUARD, | ||
'getX', | ||
'getY', | ||
'setY', | ||
'toString', | ||
]); | ||
t.is(`${wpt}`, '<4,5>'); | ||
t.is(`${wpt}`, '<5,5>'); | ||
t.is(`${wpt}`, '<6,5>'); | ||
wpt.setY(6); | ||
t.is(`${wpt}`, '<7,6>'); | ||
|
||
const otherPt = makeExoPoint(1, 2); | ||
t.false(otherPt instanceof ExoWobblyPoint); | ||
t.throws(() => apply(wpt.getX, otherPt, []), { | ||
// TODO great error message, but is a golden specific to v8 | ||
message: | ||
'"In \\"getX\\" method of (ExoWobblyPoint)" may only be applied to a valid instance: "[Alleged: ExoPoint]"', | ||
}); | ||
t.throws(() => apply(wpt.getY, otherPt, []), { | ||
// TODO great error message, but is a golden specific to v8 | ||
message: | ||
'"In \\"getY\\" method of (ExoWobblyPoint)" may only be applied to a valid instance: "[Alleged: ExoPoint]"', | ||
}); | ||
|
||
const otherWpt = makeExoWobblyPoint(3, 5, () => 1); | ||
t.is(`${otherWpt}`, '<4,5>'); | ||
t.is(apply(wpt.getX, otherWpt, []), 4); | ||
t.throws(() => apply(otherPt.getX, otherWpt, []), { | ||
// TODO great error message, but is a golden specific to v8 | ||
message: | ||
'"In \\"getX\\" method of (ExoPoint)" may only be applied to a valid instance: "[Alleged: ExoWobblyPoint]"', | ||
}); | ||
|
||
const bigWpt = makeExoWobblyPoint(-3, 5, () => 4); | ||
t.is(bigWpt.getX(), 1); | ||
// `super` calls are direct, sharing context | ||
t.is(`${bigWpt}`, '<1,5>'); | ||
}); |
Oops, something went wrong.