Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot read property: _in_call_that_will_be_reverted #16

Open
km19809 opened this issue Jun 17, 2023 · 3 comments
Open

Cannot read property: _in_call_that_will_be_reverted #16

km19809 opened this issue Jun 17, 2023 · 3 comments

Comments

@km19809
Copy link

km19809 commented Jun 17, 2023

Description

moduleImports[importName] = function (...args: any) {
const r = importValue(...args);
// This call will be reverted so it's OK if it causes a temporary desync.
if (this._in_call_that_will_be_reverted) {
return r;
}

When I call imported JS functions, this becomes undefined.
So, this._in_call_that_will_be_reverted cannot be read.

What I did
I ran the example counter but in Zig instead of AssemblyScript.

my_wasm.zig
var num: f64 = 0.0;

extern fn report_number_change(n: f64) void;

export fn increment( val: f64) void{
    num += val;
    report_number_change(num);
}

export fn multiply( val: f64) void{
    num *= val;
    report_number_change(num);
}

However, I doubt that is a Zig-related problem.
As I know, the value of the this keyword depends on its context.
Then, what is the value of this, if I call a function in a WASM?

To check that, I modified tangle.js:

              const r = importValue(...args);
              console.warn(`${importName}():${this}`)
              if (this?._in_call_that_will_be_reverted) {
                return r;
              }

Then it says:
image
The first 4 calls are exports.increment(1)s, and others are exports.multiply.callAndRevert(3)s.

This shows that wrapping a function call did not help!

wrapped_function.callAndRevert = (...args: any) => {
this._in_call_that_will_be_reverted = true;
this.call_and_revert(key, ...args);
this._in_call_that_will_be_reverted = false;

Additional note
I tried an arrow function, but it made this._in_call_that_will_be_reverted undefined, and this was not a Tangle instance. (it was a function.)
moduleImports[importName] = (...args) => {
I want to figure out why this happens, but the keyword this was nearly impossible to search.

@km19809
Copy link
Author

km19809 commented Jun 17, 2023

Ok, things get complicated.
We want this to be an instance of Tangle. It is easy to solve: we can use bind(tangle).
However, instantiation of Tangle needs a time_machine instance.
Because time_machine.setup instantiate the WASM binary, we need to modify imported functions before the setup.
It is something like the chicken-egg problem.
For now, we are trying to access a property of an uninitialized instance.
It is hard to solve...

@DiogoNeves
Copy link

Maybe I misunderstood the issue. When is the function containing _in_call_that_will_be_reverted called? If you bind, it should still have access to the time_machine once it's set?
Options I'm thinking:

  • do not require the time_machine in the constructor, although this makes it temporarily unsafe?
  • bind the importObjects from Time Machine in the Tangle constructor (makes Time Machine only assignable to 1 instance)
  • create a 3rd object with the references it needs and use it after everything is setup?

I'm sure there are better solutions, I'm just curious what would help :)

@km19809
Copy link
Author

km19809 commented Jul 15, 2023

The function containing _in_call_that_will_be_reverted is called when I call an imported function.
In addition, the function does not need a time_machine instance.
I prefer your 3rd option, but I am not sure.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants