facebook / hermes

A JavaScript engine optimized for running React Native.
https://hermesengine.dev/
MIT License
9.82k stars 630 forks source link

hermes is incompatible with numjs and cwise library #954

Closed paradite closed 1 year ago

paradite commented 1 year ago

Problem

The hermes engine is incompatible with numjs, which uses cwise library for generate cache efficient map/reduce operations.

When using numjs, some functions are dynamically generated by the library based on the dimensions of arguments provided.

For example, for the unpack function here reproduced below:

var doUnpack = cwise({
  args: ['array', 'scalar', 'index'],
  body: function unpackCwise (arr, a, idx) {
    var v = a;
    var i;
    for (i = 0; i < idx.length - 1; ++i) {
      v = v[idx[i]];
    }
    v[idx[idx.length - 1]] = arr;
  }
});

When using jsc, the functions can be generated correctly:

hermes bug normal

However, using hermes, we got an error, presumably because the function has already been compiled to bytecode, and the generated function can't make use of it:

hermes bug

Sample error for search indexing:

ReferenceError: Property '_inline_9_bytecode' doesn't exist, js engine: hermes

Solution

Using jsc can be a walkaround but with hermes going to be default, I hope this issue can be addressed with hermes.

Some ideas:

Any help on this issue would be appreciated.

Additional Context

Here's the relevant code for compiling and generating dynamic function in cwise-compiler:

https://github.com/scijs/cwise-compiler/blob/b65933f021302e15c6d4624353bb4b69168cd543/lib/compile.js#L116-L199

tmikov commented 1 year ago

If you add the directive "show source" to a function, Hermes will preserve its source code. For example:

function foo() {
  "show source";
  return 1 + 1;
}
print(foo.toString());
paradite commented 1 year ago

Thank you for the help @tmikov, let me try it out.

paradite commented 1 year ago

It seems to work, but now I am running into https://github.com/facebook/hermes/issues/612

Update: I am able to walk around #612 by triggering a hot-reload of the file that contains "show source";.

Feel free to close this issue.