BruceSherwood/glowscript

conflict with numjs since Array().join is different with origin

Closed this issue · 6 comments

I'm using glowscript and numjs (https://github.com/nicolaspanel/numjs/) at the same time, and I found I cannot use numjs when glowscript is activated. When I deactivate glow.2.7.min.js, numjs is aviable.

So I tested the code, and found Array([[1,2]]).join('\n') is different when glow.2.7.min.js is on and off. When it is off, the browser gives out:

>>>Array([[1,2]]).join('\n')
..."1,2"

When it is on, the browser gives out:

>>>Array([[1,2]]).join('\n')
..."[[1, 2]]"

And this error crashes numjs. Does this means glow.2.7.min.js change some proto functions about Array? But Array is a native object, maybe there is a bug inside glow.js

I used chrome and firefox, both have the same error.

seems Array.prototype.toString = function() { return parsearray(this) }; in glow.2.7.min.js change it.

I don't know if this line matters, but it seems fine without this line.

In lib/glow/api_misc.js the change to Array.protype.toString is preceded by the following comments:

// This string formatting machinery was originally used by both Python and JavaScript,
// but with the change to rapydscript-ng, which has its own format method, Python uses
// the rapydscript-ng method, and the following code is used only by JavaScript programs.

It had not occurred to me that this could interfere with some other JavaScript library. The intent was simply to make available to JavaScript programs the powerful formatting capabilities of Python. I don't see how I can at this late date change the GlowScript code, though perhaps you can suggest some scheme. It seems odd/undesirable that [[1,2]] should be translated to "1,2" rather than to "[[1,2]]", nor why that should cause an error in numjs.

numjs seems using a dynamic function generation like this

  var code = ["'use strict'"]
...
  code.push("proto.pick=function "+className+"_pick("+args+"){var a=[],b=[],c=this.offset")
...
  var procedure = new Function("CTOR_LIST", "ORDER", code.join("\n"))

where args is like Array(['i0', 'i1']). In general, code.join('\n') would be

proto.pick = function View1darray_pick(i0) {
...
}

but with Array.prototype.toString = function() { return parsearray(this) };, it changed to

proto.pick = function View1darray_pick(['i0']) {
,,,
}

which cannot be parsed by Function, causing a crash in numjs.

Besides, Array.prototype.toString seems to be native code. I don't think it's a good idea to change native code, which may cause unexpected error. Remove the line could fix the conflict, but it may change the output of python code in glowscript.

I think it's the problem of rapydscript-ng compiler. It should make a new Array class and rewrite the toString function, not to modify native code.

I do think that [[1,2]] to be translated to "1,2" is wired, but this is how the native goes. And both chrome and firefox goes like this.

Okay, I moved the Array.prototype from glow.2.7.min.js to the glowscript.org compiler, so that existing GlowScript JavaScript programs will continue to work as before, with the improved parsing, but standalone JavaScript programs that import the run-time glow.2.7.min.js will not be affected. Thanks for pointing out this issue.