Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

Array literals aren't Arrays #123

Open
fluidsonic opened this issue Oct 2, 2019 · 3 comments
Open

Array literals aren't Arrays #123

fluidsonic opened this issue Oct 2, 2019 · 3 comments

Comments

@fluidsonic
Copy link

fluidsonic commented Oct 2, 2019

For some reason [] instanceof Array is false when a script is run within Cloudworker.
That messes up our script :)

No idea how that can happen but in plain Node and in a Cloudflare Worker it's working correctly.

Related: nodejs/node#3425
Related: nodejs/node#11593

I guess there isn't even a way around that when using Node's VM functionality. Cloudworker cannot reliably rebuild a Cloudflare Worker environment like that. As far as I know, Cloudflare Workers use v8 isolates instead.

@hankjacobs
Copy link
Contributor

hankjacobs commented Oct 13, 2019

I’m pretty sure this has to do with an architectural issue I discuss here and improvements I made in an attempt to make the corners less sharp. It has to do with executing worker scripts within different node realms. Are you able to reproduce it in a worker with just [] instanceof Array in it?

@fluidsonic
Copy link
Author

From what I understand about Node now there is no way to fix this issue.

The Array and Object types that you pass to the context will become contextified.
Arrays created with [] and objects with {} are not contextified.
Therefor using instanceof with contextified types on non-contextified literals will never work.

Simple example:

require('vm').runInNewContext('[] instanceof Array',{Array:Array}) // false

@iameli
Copy link

iameli commented Mar 27, 2020

The way vm2 handles this is with Proxies — I don't know all the details but this apparently allows them to muck around around with the internals enough to get instanceof and everything functioning properly.

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

No branches or pull requests

3 participants