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

use core-js will throw error #346

Closed
janryWang opened this issue Oct 14, 2017 · 5 comments
Closed

use core-js will throw error #346

janryWang opened this issue Oct 14, 2017 · 5 comments

Comments

@janryWang
Copy link

janryWang commented Oct 14, 2017

this is my code

public String invokeJsWithFile(String html) throws IOException {
        Context context = Context.enter();
        context.setOptimizationLevel(-1);
        Scriptable globalScope = context.initStandardObjects();
        Reader esprimaLibReader = new InputStreamReader(getClass().getClassLoader().getResourceAsStream("test.js"));
        context.evaluateReader(globalScope, esprimaLibReader, "mozilla", 1, null);
        Function function = (Function) globalScope.get("prettierFormat", globalScope);
        Object result = function.call(context, globalScope, globalScope, new Object[] {html});
        if (result instanceof String) {
            return (String) result;
        } else if (result instanceof NativeJavaObject) {
            return (String) ((NativeJavaObject) result).getDefaultValue(String.class);
        } else if (result instanceof NativeObject) {
            return (String) ((NativeObject) result).getDefaultValue(String.class);
        }
        return result.toString();
    }

this is core-js iobject

module.exports = Object('z').propertyIsEnumerable(0) ? Object : function (it) {
  return cof(it) == 'String' ? it.split('') : Object(it);
};

Exception in thread "main" org.mozilla.javascript.EvaluatorException: Property 0 not found. (mozilla#9862)
	at org.mozilla.javascript.DefaultErrorReporter.runtimeError(DefaultErrorReporter.java:77)
	at org.mozilla.javascript.Context.reportRuntimeError(Context.java:980)
	at org.mozilla.javascript.Context.reportRuntimeError(Context.java:1035)
	at org.mozilla.javascript.Context.reportRuntimeError1(Context.java:998)
	at org.mozilla.javascript.ScriptableObject.findAttributeSlot(ScriptableObject.java:2922)
	at org.mozilla.javascript.ScriptableObject.getAttributes(ScriptableObject.java:764)
	at org.mozilla.javascript.NativeObject.execIdCall(NativeObject.java:189)
	at org.mozilla.javascript.IdFunctionObject.call(IdFunctionObject.java:101)
	at org.mozilla.javascript.Interpreter.interpretLoop(Interpreter.java:1479)
	at script(mozilla:9862)
	at script.__webpack_require__(mozilla:30)
	at script(mozilla:1900)
	at script.__webpack_require__(mozilla:30)
	at script(mozilla:19927)
	at script.__webpack_require__(mozilla:30)
	at script(mozilla:3570)
	at script.__webpack_require__(mozilla:30)
	at script(mozilla:39407)
	at script.__webpack_require__(mozilla:30)
	at script(mozilla:39397)
	at script.__webpack_require__(mozilla:30)
	at script(mozilla:39386)
	at script.__webpack_require__(mozilla:30)
	at script(mozilla:348)
	at script.__webpack_require__(mozilla:30)
	at script(mozilla:39356)
	at script.__webpack_require__(mozilla:30)
	at script(mozilla:73)
	at script(mozilla:11)
	at script.webpackUniversalModuleDefinition(mozilla:9)
	at script(mozilla:1)
	at org.mozilla.javascript.Interpreter.interpret(Interpreter.java:815)
	at org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:109)
	at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:399)
	at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3452)
	at org.mozilla.javascript.InterpretedFunction.exec(InterpretedFunction.java:120)
	at org.mozilla.javascript.Context.evaluateReader(Context.java:1275)
	at com.alibaba.relim.App.invokeJsWithFile(App.java:24)
	at com.alibaba.relim.App.main(App.java:13)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)



@gbrail
Copy link
Collaborator

gbrail commented Oct 16, 2017 via email

@whatisaphone
Copy link

The crux of the issue is that Object('z').propertyIsEnumerable(0) is throwing an exception. However, the propertyIsEnumerable method is specced to never throw (unless the object is null/undefined, which it isn't in the above case).

Source: https://tc39.github.io/ecma262/#sec-object.prototype.propertyisenumerable

The offending code is here in NativeObject. It seems to assume that if the has call returns true, getAttributes will succeed, but that assumption doesn't hold in this case.

If I manually patch core-js by replacing the offending expression with false, core-js appears at first glacne to run fine

@gbrail
Copy link
Collaborator

gbrail commented Nov 20, 2017

I wonder if maybe this is a bug in NativeString? It seems that if "has" returns true that "getAttributes" should not throw. (And if so, I wonder how many other subclasses need a similar fix?)

@gausie
Copy link
Contributor

gausie commented Jun 11, 2021

Running Object('z').propertyIsEnumerable(0) in the 1.7.13 shell seems to work fine. Since core-js now knows what Rhino is and isn't capable of, I think this can be closed?

@gbrail
Copy link
Collaborator

gbrail commented Jun 15, 2021

Sounds like this is OK to close.

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

No branches or pull requests

4 participants