@@ -21,8 +21,9 @@ export function makeCapTP(ourId, send, bootstrapObj = undefined) {
21
21
let lastQuestionID = 0 ;
22
22
23
23
const valToSlot = new WeakMap ( ) ;
24
- const slotToVal = new Map ( ) ; // exports, answers
24
+ const slotToVal = new Map ( ) ; // exports
25
25
const questions = new Map ( ) ; // chosen by us
26
+ const answers = new Map ( ) ; // chosen by our peer
26
27
const imports = new Map ( ) ; // chosen by our peer
27
28
28
29
function serializeSlot ( val , slots , slotMap ) {
@@ -70,23 +71,33 @@ export function makeCapTP(ourId, send, bootstrapObj = undefined) {
70
71
} ) ;
71
72
}
72
73
73
- function makeRemote ( slot ) {
74
+ function makeQuestion ( ) {
75
+ lastQuestionID += 1 ;
76
+ const questionID = lastQuestionID ;
77
+ const pr = makeRemote ( questionID ) ;
78
+ questions . set ( questionID , pr ) ;
79
+ return [ questionID , pr ] ;
80
+ }
81
+
82
+ function makeRemote ( target ) {
74
83
const handler = {
84
+ GET ( _o , prop ) {
85
+ const [ questionID , pr ] = makeQuestion ( ) ;
86
+ send ( {
87
+ type : 'CTP_CALL' ,
88
+ questionID,
89
+ target,
90
+ method : serialize ( harden ( [ prop ] ) ) ,
91
+ } ) ;
92
+ return harden ( pr . p ) ;
93
+ } ,
75
94
POST ( _o , prop , args ) {
76
95
// Support: o~.[prop](...args) remote method invocation
77
- // FIXME: Implement a HandledPromise here to support pipelining.
78
- const pr = { } ;
79
- pr . p = new Promise ( ( resolve , reject ) => {
80
- pr . res = resolve ;
81
- pr . rej = reject ;
82
- } ) ;
83
- lastQuestionID += 1 ;
84
- const questionID = lastQuestionID ;
85
- questions . set ( questionID , pr ) ;
96
+ const [ questionID , pr ] = makeQuestion ( ) ;
86
97
send ( {
87
98
type : 'CTP_CALL' ,
88
99
questionID,
89
- target : slot ,
100
+ target,
90
101
method : serialize ( harden ( [ prop , args ] ) ) ,
91
102
} ) ;
92
103
return harden ( pr . p ) ;
@@ -131,6 +142,7 @@ export function makeCapTP(ourId, send, bootstrapObj = undefined) {
131
142
const bootstrap =
132
143
typeof bootstrapObj === 'function' ? bootstrapObj ( ) : bootstrapObj ;
133
144
// console.log('sending bootstrap', bootstrap);
145
+ answers . set ( questionID , bootstrap ) ;
134
146
send ( {
135
147
type : 'CTP_RETURN' ,
136
148
answerID : questionID ,
@@ -140,15 +152,23 @@ export function makeCapTP(ourId, send, bootstrapObj = undefined) {
140
152
CTP_CALL ( obj ) {
141
153
const { questionID, target } = obj ;
142
154
const [ prop , args ] = unserialize ( obj . method ) ;
143
- const val = unserialize ( {
144
- body : JSON . stringify ( {
145
- [ QCLASS ] : 'slot' ,
146
- index : 0 ,
147
- } ) ,
148
- slots : [ target ] ,
149
- } ) ;
150
- HandledPromise . applyMethod ( val , prop , args )
151
- . then ( res =>
155
+ let val ;
156
+ if ( answers . has ( target ) ) {
157
+ val = answers . get ( target ) ;
158
+ } else {
159
+ val = unserialize ( {
160
+ body : JSON . stringify ( {
161
+ [ QCLASS ] : 'slot' ,
162
+ index : 0 ,
163
+ } ) ,
164
+ slots : [ target ] ,
165
+ } ) ;
166
+ }
167
+ const hp = args
168
+ ? HandledPromise . applyMethod ( val , prop , args )
169
+ : HandledPromise . get ( val , prop ) ;
170
+ answers . set ( questionID , hp ) ;
171
+ hp . then ( res =>
152
172
send ( {
153
173
type : 'CTP_RETURN' ,
154
174
answerID : questionID ,
@@ -198,14 +218,7 @@ export function makeCapTP(ourId, send, bootstrapObj = undefined) {
198
218
199
219
// Get a reference to the other side's bootstrap object.
200
220
const getBootstrap = ( ) => {
201
- const pr = { } ;
202
- pr . p = new Promise ( ( resolve , reject ) => {
203
- pr . res = resolve ;
204
- pr . rej = reject ;
205
- } ) ;
206
- lastQuestionID += 1 ;
207
- const questionID = lastQuestionID ;
208
- questions . set ( questionID , pr ) ;
221
+ const [ questionID , pr ] = makeQuestion ( ) ;
209
222
send ( {
210
223
type : 'CTP_BOOTSTRAP' ,
211
224
questionID,
0 commit comments