You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import{PString,RString}from'../../../tests/cbh.js';// ...constPString$1=classPString{constructor(arg0){console.error(`[module="jcbhmr:hello-world-rust-wasm-component-lib/cb", function="[constructor]p-string"] call cb=${arguments[0]}`);if(!(arg0instanceofPString)){thrownewError('Resource error: Not a valid "HPString" resource.');}varhandle0=handleCnt0++;handleTable0.set(handle0,{rep: arg0,own: true});constret=exports1['jcbhmr:hello-world-rust-wasm-component-lib/cb#[constructor]p-string'](handle0);console.error(`[module="jcbhmr:hello-world-rust-wasm-component-lib/cb", function="[constructor]p-string"] return result=${toResultString(ret)}`);varhandle2=ret;varrsc1=new.target===PString$1 ? this : Object.create(PString$1.prototype);varrep3=handleTable2.get(handle2).rep;Object.defineProperty(rsc1,resourceHandleSymbol,{writable: true,value: rep3});finalizationRegistry2.register(rsc1,handle2,rsc1);Object.defineProperty(rsc1,symbolDispose,{writable: true,value: function(){}});handleTable2.delete(handle2);returnrsc1;}}
When inside the class PString { } for the exported guest PString resource type it checks that the arg0 is instanceof PString. That's SUPPOSED to check that arg0 is an instance of the host'sPString class. Instead due to shadowing, it's referring to itself (the class PString {} declaration that it's inside of). This is incorrect behaviour.
Instead, the code should look like something like this:
import{PString,RString}from'../../../tests/cbh.js';// ...constPString$1=class{static{Object.defineProperty(this,'name',{value: 'PString',configurable: true})}constructor(arg0){console.error(`[module="jcbhmr:hello-world-rust-wasm-component-lib/cb", function="[constructor]p-string"] call cb=${arguments[0]}`);if(!(arg0instanceofPString)){thrownewError('Resource error: Not a valid "HPString" resource.');}varhandle0=handleCnt0++;handleTable0.set(handle0,{rep: arg0,own: true});constret=exports1['jcbhmr:hello-world-rust-wasm-component-lib/cb#[constructor]p-string'](handle0);console.error(`[module="jcbhmr:hello-world-rust-wasm-component-lib/cb", function="[constructor]p-string"] return result=${toResultString(ret)}`);varhandle2=ret;varrsc1=new.target===PString$1 ? this : Object.create(PString$1.prototype);varrep3=handleTable2.get(handle2).rep;Object.defineProperty(rsc1,resourceHandleSymbol,{writable: true,value: rep3});finalizationRegistry2.register(rsc1,handle2,rsc1);Object.defineProperty(rsc1,symbolDispose,{writable: true,value: function(){}});handleTable2.delete(handle2);returnrsc1;}}
OR like this
import{PStringas$host_PString,RStringas$host_RString}from'../../../tests/cbh.js';// ...constPString$1=classPString{constructor(arg0){console.error(`[module="jcbhmr:hello-world-rust-wasm-component-lib/cb", function="[constructor]p-string"] call cb=${arguments[0]}`);if(!(arg0instanceof$host_PString)){thrownewError('Resource error: Not a valid "HPString" resource.');}varhandle0=handleCnt0++;handleTable0.set(handle0,{rep: arg0,own: true});constret=exports1['jcbhmr:hello-world-rust-wasm-component-lib/cb#[constructor]p-string'](handle0);console.error(`[module="jcbhmr:hello-world-rust-wasm-component-lib/cb", function="[constructor]p-string"] return result=${toResultString(ret)}`);varhandle2=ret;varrsc1=new.target===PString$1 ? this : Object.create(PString$1.prototype);varrep3=handleTable2.get(handle2).rep;Object.defineProperty(rsc1,resourceHandleSymbol,{writable: true,value: rep3});finalizationRegistry2.register(rsc1,handle2,rsc1);Object.defineProperty(rsc1,symbolDispose,{writable: true,value: function(){}});handleTable2.delete(handle2);returnrsc1;}}
Notice how now the instanceoof PString refers to the OUTER host-defined PString class.
Suggestion:
Rename the class using Object.defineProperty() either in static {} block or after defining it.
OR mangle the names of imports like { PString as $host_PString } to avoid having them be shadowed by local export declarations.
The text was updated successfully, but these errors were encountered:
jcbhmr
changed the title
Local exported MyResource shadows imported host MyResource which causes instanceof MyResource to always fail
Local function/class name shadows imported host item of same name
Jan 15, 2024
Thanks for the detailed report on this. I've gone with the class PString$1 { ... } approach here instead of const PString$1 = class PString{, with the fix in #340.
Specific example:
When inside the
class PString { }
for the exported guestPString
resource type it checks that thearg0
isinstanceof PString
. That's SUPPOSED to check thatarg0
is an instance of the host'sPString
class. Instead due to shadowing, it's referring to itself (theclass PString {}
declaration that it's inside of). This is incorrect behaviour.Instead, the code should look like something like this:
OR like this
Notice how now the
instanceoof PString
refers to the OUTER host-defined PString class.Suggestion:
Rename the class using
Object.defineProperty()
either instatic {}
block or after defining it.OR mangle the names of imports like
{ PString as $host_PString }
to avoid having them be shadowed by local export declarations.The text was updated successfully, but these errors were encountered: