@@ -35,7 +35,7 @@ export module DotNet {
3535 }
3636
3737 class JSObject {
38- // TODO(oroztocil): Is it correct to cache functions/properties when they can change?
38+ // We cache resolved members. Note that this means we can return stale values if the object has been modified since then.
3939 _cachedMembers : Map < string , ObjectMemberDescriptor > ;
4040
4141 // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -78,7 +78,8 @@ export module DotNet {
7878
7979 const result = { parent, name : lastKey } as ObjectMemberDescriptor ;
8080
81- if ( typeof current === "function" ) {
81+ if ( current instanceof Function ) {
82+ // If the member is a function, we bind the parent object as its "this" context.
8283 result . func = current . bind ( parent ) ;
8384 }
8485
@@ -452,15 +453,15 @@ export module DotNet {
452453 try {
453454 const valueOrPromise = this . handleJSCall ( targetInstanceId , identifier , callType , argsJson ) ;
454455
455- // We only listen for a result if the caller wants to be notified about it
456+ // We only await the result if the caller wants to be notified about it
456457 if ( asyncHandle ) {
457- // On completion, dispatch result back to .NET
458458 const result = await valueOrPromise ;
459459 const serializedResult = stringifyArgs ( this , [
460460 asyncHandle ,
461461 true ,
462462 createJSCallResult ( result , resultType )
463463 ] ) ;
464+ // On success, dispatch result back to .NET
464465 this . _dotNetCallDispatcher . endInvokeJSFromDotNet ( asyncHandle , true , serializedResult ) ;
465466 }
466467 } catch ( error : any ) {
@@ -470,6 +471,7 @@ export module DotNet {
470471 false ,
471472 formatError ( error )
472473 ] ) ;
474+ // On failure, dispatch error back to .NET
473475 this . _dotNetCallDispatcher . endInvokeJSFromDotNet ( asyncHandle , false , serializedError ) ;
474476 }
475477 }
@@ -496,15 +498,15 @@ export module DotNet {
496498 }
497499
498500 handleJSFunctionCall ( identifier : string , func : any , args : unknown [ ] ) : any {
499- if ( typeof func === "function" ) {
501+ if ( func instanceof Function ) {
500502 return func ( ...args ) ;
501503 } else {
502504 throw new Error ( `The value '${ identifier } ' is not a function.` ) ;
503505 }
504506 }
505507
506508 handleJSNewCall ( identifier : string , func : any , args : unknown [ ] ) : any {
507- if ( typeof func === "function" ) {
509+ if ( func instanceof Function ) {
508510 try {
509511 // The new expression throws if the function is not constructible (e.g. an arrow function).
510512 return new func ( ...args ) ;
0 commit comments