Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

ReferenceError: "importPackage" is not defined. #657

Closed
CanIGetaPR opened this issue Apr 5, 2020 · 5 comments
Closed

ReferenceError: "importPackage" is not defined. #657

CanIGetaPR opened this issue Apr 5, 2020 · 5 comments

Comments

@CanIGetaPR
Copy link

Rhino Version: rhino-1.7.12.jar

Exception: org.mozilla.javascript.EcmaError: ReferenceError: "importPackage" is not defined.

Stack trace:

        at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:4236)
	at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:4214)
	at org.mozilla.javascript.ScriptRuntime.notFoundError(ScriptRuntime.java:4304)
	at org.mozilla.javascript.ScriptRuntime.getNameFunctionAndThis(ScriptRuntime.java:2441)
	at org.mozilla.javascript.optimizer.OptRuntime.callName(OptRuntime.java:74)
	at org.mozilla.javascript.gen.script_js_1._c_script_0(script.js:0)
	at org.mozilla.javascript.gen.script_js_1.call(script.js)
	at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:412)
	at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3545)
	at org.mozilla.javascript.gen.script_js_1.call(script.js)
	at org.mozilla.javascript.gen.script_js_1.exec(script.js)
	at getScope(User Source File)

Reproducing code:

private static Scriptable getScope(Script script) {
        Context ctx = Context.enter();
        try {
            Scriptable scope = ctx.initStandardObjects(null);
            script.exec(ctx, scope);
            return scope;
        } finally {
            Context.exit();
        }
    }

Source javascript at line 1:

importPackage(Packages.main.data);

The script argument is previously generated by ctx.compileReader(fileReader, name, 0, null);.

To my understanding, importPackage is part of LiveConnect, and that gets lazy loaded into scopes at some point. It seems to not get lazy loaded in my case.

@CanIGetaPR
Copy link
Author

I fixed my own problem by searching the source code for importPackage and finding ImporterTopLevel thus

ScriptableObject scope = new ImporterTopLevel(ctx);
scope = ctx.initStandardObjects(scopel);

But this required far too much effort, there should be a default inifStandardObjectsWithLiveConnect method under context.

@p-bakker
Copy link
Collaborator

importClass & importPackage are enabled by default in the Shell.

If you're embedding Rhino, its up to you to decide what to init.

If you want importClass and importPackage, you have 2 options:

  1. add them to an existing scope using ImporterTopLevel.init(Context cx, Scriptable scope, boolean sealed)
  2. create your topLevel scope using the ImporterTopLevel(Context cx) constructor: this will init the standard objects and add the two import functions

@dazoulay-simplicite
Copy link

Hello,

How can importClass/importPackage can be enabled when using Rhino from a strict JSR-223 code ?

Using previous versions up to 1.7.13 along with the cat.inspiracio.rhino-js-engine JSR-223 adapter did not require anything particular : these import methods where available by default.

Version 1.7.14 seems now to be incompatible with the above JSR-223 adapter.

And using the new org.mozilla.rhino-engine JSR-223 adapter is causing these "ImportPackage is not defined" exceptions (same issue with both packages in version 1.7.13)

Here is my test code:

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("rhino");
ScriptContext ctx = new SimpleScriptContext();
Bindings bindings = ctx.getBindings(ScriptContext.ENGINE_SCOPE);
bindings.put("who", "David");
System.out.println(engine.eval("(function() { return 'Hello ' + who; })();", bindings)); // Success
System.out.println(engine.eval("importPackage(Packages.org.json);"
  + "(function() { return new JSONObject().put('hello', who).toString(); })();", bindings)); // Exception

and the corresponding pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>test</groupId>
  <artifactId>rhino</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
    <dependency>
      <groupId>org.mozilla</groupId>
      <artifactId>rhino</artifactId>
      <version>1.7.14</version>
    </dependency>
    <dependency>
      <groupId>org.mozilla</groupId>
      <artifactId>rhino-engine</artifactId>
      <version>1.7.14</version>
    </dependency>
    <!-- Just here to test importing an additonal package -->
    <dependency>
      <groupId>org.json</groupId>
      <artifactId>json</artifactId>
      <version>20211205</version>
    </dependency>
  </dependencies>
</project>

Thank you for your help or advice.

@p-bakker
Copy link
Collaborator

p-bakker commented Mar 9, 2022

See #1043

@panxin2000
Copy link

我通过搜索 importPackage 的源代码并找到 ImporterTopLevel 解决了我自己的问题

ScriptableObject scope = new ImporterTopLevel(ctx);
scope = ctx.initStandardObjects(scopel);

但这需要太多的努力,上下文中应该有一个默认的 inifStandardObjectsWithLiveConnect 方法。

这类文档是在哪里查看的呢?

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

No branches or pull requests

4 participants