-
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
6 changed files
with
287 additions
and
31 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,95 @@ | ||
/* 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, Far } 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'; | ||
|
||
/** | ||
* Classes whose instances should be Far objects may find it convenient to | ||
* inherit from this base class. Note that the constructor of this base class | ||
* freezes the instance in an empty state, so all is interesting attributes | ||
* can only depend on its identity and what it inherits from. | ||
* This includes private fields, as those are keyed only on | ||
* this object's identity. However, we discourage (but cannot prevent) such | ||
* use of private fields, as they cannot easily be refactored into Exo state. | ||
*/ | ||
const FarBaseClass = class FarBaseClass { | ||
constructor() { | ||
harden(this); | ||
} | ||
}; | ||
|
||
Far('FarBaseClass', FarBaseClass.prototype); | ||
harden(FarBaseClass); | ||
|
||
// Based on FarSubclass1 in test-far-class-instances.js | ||
class DoublerBehaviorClass extends FarBaseClass { | ||
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,74 @@ | ||
// eslint-disable-next-line import/order | ||
import { test } from './prepare-test-env-ava.js'; | ||
|
||
// eslint-disable-next-line import/order | ||
import { getInterfaceMethodKeys, M, objectMetaMap } from '@endo/patterns'; | ||
import { defineExoClass } from '../src/exo-makers.js'; | ||
import { GET_INTERFACE_GUARD } from '../src/exo-tools.js'; | ||
|
||
const { getPrototypeOf } = Object; | ||
|
||
const UpCounterI = M.interface('UpCounter', { | ||
incr: M.call() | ||
// TODO M.number() should not be needed to get a better error message | ||
.optional(M.and(M.number(), M.gte(0))) | ||
.returns(M.number()), | ||
}); | ||
|
||
const denumerate = obj => | ||
objectMetaMap( | ||
obj, | ||
desc => ({ ...desc, enumerable: false }), | ||
getPrototypeOf(obj), | ||
); | ||
|
||
test('test defineExoClass', t => { | ||
const makeUpCounter = defineExoClass( | ||
'UpCounter', | ||
UpCounterI, | ||
/** @param {number} x */ | ||
(x = 0) => ({ x }), | ||
denumerate({ | ||
incr(y = 1) { | ||
const { state } = this; | ||
state.x += y; | ||
return state.x; | ||
}, | ||
}), | ||
); | ||
const upCounter = makeUpCounter(3); | ||
t.is(upCounter.incr(5), 8); | ||
t.is(upCounter.incr(1), 9); | ||
t.throws(() => upCounter.incr(-3), { | ||
message: 'In "incr" method of (UpCounter): arg 0?: -3 - Must be >= 0', | ||
}); | ||
// @ts-expect-error bad arg | ||
t.throws(() => upCounter.incr('foo'), { | ||
message: | ||
'In "incr" method of (UpCounter): arg 0?: string "foo" - Must be a number', | ||
}); | ||
t.deepEqual(upCounter[GET_INTERFACE_GUARD](), UpCounterI); | ||
t.deepEqual(getInterfaceMethodKeys(UpCounterI), ['incr']); | ||
|
||
const symbolic = Symbol.for('symbolic'); | ||
const FooI = M.interface('Foo', { | ||
m: M.call().returns(), | ||
[symbolic]: M.call(M.boolean()).returns(), | ||
}); | ||
t.deepEqual(getInterfaceMethodKeys(FooI), ['m', Symbol.for('symbolic')]); | ||
const makeFoo = defineExoClass( | ||
'Foo', | ||
FooI, | ||
() => ({}), | ||
denumerate({ | ||
m() {}, | ||
[symbolic]() {}, | ||
}), | ||
); | ||
const foo = makeFoo(); | ||
t.deepEqual(foo[GET_INTERFACE_GUARD](), FooI); | ||
t.throws(() => foo[symbolic]('invalid arg'), { | ||
message: | ||
'In "[Symbol(symbolic)]" method of (Foo): arg 0: string "invalid arg" - Must be a boolean', | ||
}); | ||
}); |
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