godotjs / javascript

Javascript binding for godotengine
https://godotjs.github.io/
MIT License
980 stars 82 forks source link

Unable to rebind ECMAScript instance from godot object in threaded worker.js #108

Open dangerousbeans opened 3 years ago

dangerousbeans commented 3 years ago

My main thread is sending out a ECMAscript class as a message to a worker thread using godot.abandon_value & godot.adopt_value, but I only get the godot object and cannot call any of the js methods on the class:

// parcel.tsx
export default class Parcel extends godot.Spatial {
  test_function(){
    console.log("stuff I wana run here"))
  }
}

// worker.js
onmessage = function(e) {
  let parcel = godot.adopt_value(e.parcel)
  console.log(parcel)                                 // [Spatial:2362]
  console.log(parcel.test_function())         // undefined
  console.log(parcel.call("test_function")) // error
}

// grid.tsx
export default class Grid  extends godot.Spatial {
  featureLoadThread

  _ready(){
    let p = new Parcel()

    this.featureLoadThread = new Worker('res://src/worker.js')
    this.featureLoadThread.onmessage = this._onmessage.bind(this);
    this.featureLoadThread.postMessage({ type:"featureLoad", parcel: godot.abandon_value(p) })
  }
}
dangerousbeans commented 3 years ago

Here's an example of the situation applied to the demos project; https://github.com/dangerousbeans/ECMAScriptDemos/commit/6959fbf385fd074037e3c23d0a21ddb06aac1502

Geequlim commented 3 years ago

You can only pass primitive values between workers. It is the standard and limit of the JavaScript language, this why the adopt_value exists.

Joran Kikke @.***>于2021年8月15日 周日10:00写道:

Here's an example of the situation applied to the demos project; @.*** https://github.com/dangerousbeans/ECMAScriptDemos/commit/6959fbf385fd074037e3c23d0a21ddb06aac1502

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/GodotExplorer/ECMAScript/issues/108#issuecomment-898983282, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABVEKTFPXZAYKK34XKJAYQTT44NSVANCNFSM5CFURLUA .

dangerousbeans commented 3 years ago

@Geequlim I can get the godot object fine, but the one returned from godot.adopt_value no longer has any ECMAscript bindings or js context