core icon indicating copy to clipboard operation
core copied to clipboard

Array functions don't work

Open NexelOfficial opened this issue 1 year ago • 9 comments

When using array.includes() in JavaScript, the code doesn't convert to Java functions properly. Server error: TypeError: o.includes is not a function

NexelOfficial avatar Jun 24 '24 17:06 NexelOfficial

Hi @NexelOfficial, could you provide a snippet of the code? I'm happy to look into this.

connerdouglass avatar Jun 24 '24 17:06 connerdouglass

Sure. Take this very basic example that doesn't work:

const arr = ["itemA", "itemB"];

if (arr.includes("itemA")) {
  console.log("It's in!");
}

I think this has to do with nashorn not being set to ES6 mode. By the way, did you guys get my email? I would love to contribute to this project.

NexelOfficial avatar Jun 24 '24 18:06 NexelOfficial

There's a great improvement @NexelOfficial ... let us move beyond Nashorn!

mindflowgo avatar Jun 25 '24 08:06 mindflowgo

I don't know if there are good insights available on Walterhiggens one ( https://github.com/walterhiggins/ScriptCraft/tree/development/src/main/java/bukkit/org/scriptcraftjs), but let's try to keep all those improvements in this one. As long as we don't get the core system bloated!

mindflowgo avatar Jun 25 '24 08:06 mindflowgo

@NexelOfficial I think we're on the right track with Nashorn. But the core library needs some polyfills for full ES6 compatibility. For this specific case...

Array.prototype.includes = function(searchElement: any, fromIndex?: number) {
    return this.indexOf(searchElement, fromIndex) !== -1;
};

We could use lots of help. Our email account was just setup after being down for a while. Please forward your email to [email protected] again and we should receive it!

connerdouglass avatar Jun 25 '24 08:06 connerdouglass

Pull request to add this polyfill: https://github.com/customrealms/core/pull/43

connerdouglass avatar Jun 25 '24 09:06 connerdouglass

Changing the target to ES2015 in the tsconfig might also work. Array.prototype.includes was introduced in ES2016 and I think the default Nashorn version is ES2015. Might be worth checking out.

Edit: From the Nashorn wiki page: It provides a 100% support of ECMAScript 5.1.[9] It was the first JavaScript implementation to achieve 100% pass rate on the ECMAScript 5.1 test suite.[10]

ES5 it is!

NexelOfficial avatar Jun 25 '24 11:06 NexelOfficial

@connerdouglass Could you please generate a new link to the Discord and send it to me? The old one is expired.

NexelOfficial avatar Jun 25 '24 13:06 NexelOfficial

@NexelOfficial https://discord.gg/bsTearKQsm

connerdouglass avatar Jun 25 '24 14:06 connerdouglass