Skip to content

Commit e3e75da

Browse files
committed
expose newProxy() on RemoteObject
1 parent 7391ca0 commit e3e75da

File tree

1 file changed

+33
-27
lines changed

1 file changed

+33
-27
lines changed

lib/node/node-wsh.js

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@ export class WindowsScriptingHost extends EventEmitter {
2121
#finalizer = new FinalizationRegistry( this.#finalized.bind( this));
2222
#ref2proxy = new Map(); // Map< string, WeakRef< Proxy | <custom-mapped>>>
2323
#proxy2ref = new WeakMap(); // Map< Proxy | <custom-mapped>, string>
24-
#handler = {
25-
get: ( ticket, prop) => ticket.get( prop, undefined),
26-
set: ( ticket, prop, value) => ticket.set( prop, value),
27-
apply: ( ticket, thisArg, argumentsList) => ticket.apply( thisArg, argumentsList, undefined),
28-
construct: ( ticket, argumentsList) => ticket.construct( argumentsList, undefined),
29-
};
3024

3125
constructor( syncline) {
3226
super();
@@ -139,11 +133,9 @@ export class WindowsScriptingHost extends EventEmitter {
139133
if( existingProxy)
140134
return existingProxy;
141135

142-
const ticket = new RemoteObject( this.#syncline, { encode: this.#encode.bind( this), decode: this.#decode.bind( this)}, ref);
143-
const newProxy = mapper? mapper( ticket): new Proxy( ticket, this.#handler);
136+
const newProxy = new RemoteObject( this.#syncline, this.#encode.bind( this), this.#decode.bind( this), ref, mapper).proxy;
144137
if( this.#proxy2ref.has( newProxy)) // sanity check, doesn't catch all misbehaving mappers
145138
throw new Error( `mapper must return new objects: ${ newProxy}`);
146-
ticket.setProxy( newProxy);
147139
this.#ref2proxy.set( ref, new WeakRef( newProxy)); // may be overwriting a dead WeakRef
148140
this.#proxy2ref.set( newProxy, ref);
149141
this.#finalizer.register( newProxy, ref);
@@ -162,39 +154,53 @@ export class WindowsScriptingHost extends EventEmitter {
162154

163155
class RemoteObject extends Function {
164156

157+
static #handler = {
158+
get: ( ticket, prop) => ticket.get( prop, undefined),
159+
set: ( ticket, prop, value) => ticket.set( prop, value),
160+
apply: ( ticket, thisArg, argumentsList) => ticket.apply( thisArg, argumentsList, undefined),
161+
construct: ( ticket, argumentsList) => ticket.construct( argumentsList, undefined),
162+
};
163+
165164
#syncline;
166-
#codec;
165+
#encode;
166+
#decode;
167167
#ref;
168168
#proxy;
169169

170-
constructor( syncline, codec, ref) {
170+
constructor( syncline, encode, decode, ref, mapper) {
171171
super();
172172
this.#syncline = syncline;
173-
this.#codec = codec;
173+
this.#encode = encode;
174+
this.#decode = decode;
174175
this.#ref = ref;
176+
this.#proxy = mapper? mapper( this): this.newProxy();
177+
}
178+
179+
get proxy() {
180+
return this.#proxy;
175181
}
176182

177-
setProxy( proxy) {
178-
this.#proxy = proxy;
183+
newProxy() {
184+
return new Proxy( this, RemoteObject.#handler);
179185
}
180186

181187
get( prop, mapper) {
182188
if( prop === Symbol.toPrimitive)
183189
return () => `ref#${ this.#ref}`;
184-
const encodedTarget = this.#codec.encode( this.#proxy);
185-
const encodedProp = this.#codec.encode( prop);
190+
const encodedTarget = this.#encode( this.#proxy);
191+
const encodedProp = this.#encode( prop);
186192
const output = JSON.parse( this.#syncline.exchange( JSON.stringify( [ 'get', encodedTarget, encodedProp])));
187193
switch( output[ 0]) {
188-
case 'value': return this.#codec.decode( output[ 1], mapper);
194+
case 'value': return this.#decode( output[ 1], mapper);
189195
case 'error': throw new Error( output[ 1]);
190196
default: throw new Error( `unknown status: ${ output[ 0]}`);
191197
}
192198
}
193199

194200
set( prop, value) {
195-
const encodedTarget = this.#codec.encode( this.#proxy);
196-
const encodedProp = this.#codec.encode( prop);
197-
const encodedValue = this.#codec.encode( value);
201+
const encodedTarget = this.#encode( this.#proxy);
202+
const encodedProp = this.#encode( prop);
203+
const encodedValue = this.#encode( value);
198204
const output = JSON.parse( this.#syncline.exchange( JSON.stringify( [ 'set', encodedTarget, encodedProp, encodedValue])));
199205
switch( output[ 0]) {
200206
case 'set': return true;
@@ -204,23 +210,23 @@ class RemoteObject extends Function {
204210
}
205211

206212
apply( thisArg, argumentsList, mapper) {
207-
const encodedTarget = this.#codec.encode( this.#proxy);
208-
const encodedThisArg = this.#codec.encode( thisArg);
209-
const encodedArgumentList = this.#codec.encode( [ ...argumentsList]); // argumentsList may not be instanceof Array
213+
const encodedTarget = this.#encode( this.#proxy);
214+
const encodedThisArg = this.#encode( thisArg);
215+
const encodedArgumentList = this.#encode( [ ...argumentsList]); // argumentsList may not be instanceof Array
210216
const output = JSON.parse( this.#syncline.exchange( JSON.stringify( [ 'apply', encodedTarget, encodedThisArg, encodedArgumentList])));
211217
switch( output[ 0]) {
212-
case 'value': return this.#codec.decode( output[ 1], mapper);
218+
case 'value': return this.#decode( output[ 1], mapper);
213219
case 'error': throw new Error( output[ 1]);
214220
default: throw new Error( `unknown status: ${ output[ 0]}`);
215221
}
216222
}
217223

218224
construct( argumentsList, mapper) {
219-
const encodedTarget = this.#codec.encode( this.#proxy);
220-
const encodedArgumentList = this.#codec.encode( [ ...argumentsList]); // argumentsList may not be instanceof Array
225+
const encodedTarget = this.#encode( this.#proxy);
226+
const encodedArgumentList = this.#encode( [ ...argumentsList]); // argumentsList may not be instanceof Array
221227
const output = JSON.parse( this.#syncline.exchange( JSON.stringify( [ 'construct', encodedTarget, encodedArgumentList])));
222228
switch( output[ 0]) {
223-
case 'value': return this.#codec.decode( output[ 1], mapper);
229+
case 'value': return this.#decode( output[ 1], mapper);
224230
case 'error': throw new Error( output[ 1]);
225231
default: throw new Error( `unknown status: ${ output[ 0]}`);
226232
}

0 commit comments

Comments
 (0)