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

extern functions behave differently in Wing simulator #4131

Closed
Chriscbr opened this issue Sep 10, 2023 · 8 comments · Fixed by #5554
Closed

extern functions behave differently in Wing simulator #4131

Chriscbr opened this issue Sep 10, 2023 · 8 comments · Fixed by #5554
Assignees
Labels
🐛 bug Something isn't working 🛠️ compiler Compiler 🕹️ simulator Related to the `sim` compilation target

Comments

@Chriscbr
Copy link
Contributor

I tried this:

I made a Wing project with these two files, and ran wing test:

// main.w
class Util {
  extern "./superjson.js" static inflight dostuff();
}

test "superjson" {
  Util.dostuff();
}

// superjson.js
const superjson = require("superjson");

exports.dostuff = function() {
    const string = superjson.stringify(
        { handle: 'sim-3', method: 'dec', args: [ undefined, 'my-key' ] }
    );
    console.log(string);
}

This happened:

This is printed by the test:

{"json":{"handle":"sim-3","method":"dec","args":[null,"my-key"]},"meta":{"referentialEqualities":{}}}

I expected this:

I expected the output to match the output when I run superjson in a Node REPL:

Welcome to Node.js v18.14.1.
Type ".help" for more information.
> require("superjson").stringify({ handle: 'sim-3', method: 'dec', args: [ undefined, 'my-key' ] })
'{"json":{"handle":"sim-3","method":"dec","args":[null,"my-key"]},"meta":{"values":{"args.0":["undefined"]}}}'

Notice that the "meta" value contains an annotation saying that args.0 needs to be parsed as "undefined" instead of null. This annotation is not added when running in Wing simulator.

Is there a workaround?

No response

Component

SDK

Wing Version

0.29.11

Node.js Version

18.14.1

Platform(s)

MacOS

Anything else?

No response

Community Notes

  • Please vote by adding a 👍 reaction to the issue to help us prioritize.
  • If you are interested to work on this issue, please leave a comment.
@Chriscbr Chriscbr added 🐛 bug Something isn't working 🛠️ compiler Compiler labels Sep 10, 2023
@Chriscbr Chriscbr added the 🕹️ simulator Related to the `sim` compilation target label Oct 2, 2023
@Chriscbr Chriscbr changed the title npm library (superjson) behaves differently in Wing simulator extern functions behave differently in Wing simulator Oct 2, 2023
@Chriscbr
Copy link
Contributor Author

Chriscbr commented Oct 2, 2023

Another example of an extern function that runs fine in Node.js, but throws an error in Wing:

exports.dostuff = function() {
    const assert = require("assert");
    const string = "{\"key1\":1,\"key2\":2}";
    const obj = ({"key1": 1,"key2": 2});
    assert.deepStrictEqual(JSON.parse(string), obj);
}

Due to this bug, we see different equality behavior between preflight and inflight:

let s = "{\"key1\":1,\"key2\":2}";
let obj = Json { "key1": 1, "key2": 2 };

assert(Json.parse(s) == obj); // OK

test "deepCopy()" {
  let s = "{\"key1\":1,\"key2\":2}";
  let obj = Json { "key1": 1, "key2": 2 };

  assert(Json.parse(s) == obj); // error
}

@Chriscbr
Copy link
Contributor Author

Chriscbr commented Oct 2, 2023

Maybe adding assert to the VM context will fix this?

const context = vm.createContext({

@staycoolcall911
Copy link
Contributor

Changed priority p3 -> p1

@Chriscbr
Copy link
Contributor Author

In the Json equality example above, Wing is swallowing the error message (it just reports assertion failed: Json.parse(s) == obj). This Node.js code reproduces the full error:

let code = `
const j = { key1: 1, key2: 2 };
const k = "{\\"key1\\":1,\\"key2\\":2}";
require('assert').deepStrictEqual(j, JSON.parse(k));
console.log("done");
`
eval(code);

let vm = require("vm").runInNewContext(code, {
    require,
    console,
    JSON,
});
console.log(vm);

When eval() is called on code, it works, but when code is executed by the vm it throws an error like this:

node:assert:124
  throw new AssertionError(obj);
  ^

AssertionError [ERR_ASSERTION]: Values have same structure but are not reference-equal:

{
  key1: 1,
  key2: 2
}

    at evalmachine.<anonymous>:4:19
    at Script.runInContext (node:vm:141:12)
    at Script.runInNewContext (node:vm:146:17)
    at Object.runInNewContext (node:vm:300:38)
    at Object.<anonymous> (/Users/chrisr/dev/wing-test/vm.js:9:24)
    at Module._compile (node:internal/modules/cjs/loader:1254:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
    at Module.load (node:internal/modules/cjs/loader:1117:32)
    at Module._load (node:internal/modules/cjs/loader:958:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) {
  generatedMessage: true,
  code: 'ERR_ASSERTION',
  actual: { key1: 1, key2: 2 },
  expected: { key1: 1, key2: 2 },
  operator: 'deepStrictEqual'
}

@Chriscbr
Copy link
Contributor Author

Related to #4118

Copy link

Hi,

This issue hasn't seen activity in 60 days. Therefore, we are marking this issue as stale for now. It will be closed after 7 days.
Feel free to re-open this issue when there's an update or relevant information to be added.
Thanks!

@github-actions github-actions bot added the Stale label Dec 20, 2023
@garysassano
Copy link
Collaborator

Keep.

@monadabot
Copy link
Contributor

Congrats! 🚀 This was released in Wing 0.59.43.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 bug Something isn't working 🛠️ compiler Compiler 🕹️ simulator Related to the `sim` compilation target
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

4 participants