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

Skip importing same class twice (fixes #1463) #1676

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,10 @@ private static void importClass(Scriptable scope, NativeJavaClass cl) {
String s = cl.getClassObject().getName();
String n = s.substring(s.lastIndexOf('.') + 1);
Object val = scope.get(n, scope);
if (val != NOT_FOUND && val != cl) {
if (val != NOT_FOUND) {
if (val.equals(cl)) {
return; // do not redefine same class
}
throw Context.reportRuntimeErrorById("msg.prop.defined", n);
}
// defineProperty(n, cl, DONTENUM);
Expand Down
106 changes: 106 additions & 0 deletions tests/src/test/java/org/mozilla/javascript/tests/ImportClassTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/** */
package org.mozilla.javascript.tests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.UUID;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.ContextFactory;
import org.mozilla.javascript.ImporterTopLevel;
import org.mozilla.javascript.NativeJavaClass;
import org.mozilla.javascript.Script;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
import org.mozilla.javascript.UniqueTag;
import org.mozilla.javascript.drivers.TestUtils;
import org.mozilla.javascript.tools.shell.Global;
import org.mozilla.javascript.tools.shell.ShellContextFactory;

/**
* @author donnamalayeri
*/
public class ImportClassTest {

protected final Global global = new Global();

public ImportClassTest() {
global.init(contextFactory);
}

@Before
public void setUp() {
TestUtils.setGlobalContextFactory(contextFactory);
}

@After
public void tearDown() {
TestUtils.setGlobalContextFactory(null);
}

private ContextFactory contextFactory =
new ShellContextFactory() {
@Override
protected boolean hasFeature(Context cx, int featureIndex) {
if (featureIndex == Context.FEATURE_ENHANCED_JAVA_ACCESS) return true;
return super.hasFeature(cx, featureIndex);
}
};

@Test
public void importPackageAndClass() {
Object result =
runScript(
"importPackage(java.util);\n"
+ "UUID.randomUUID();\n" // calls getPkgProperty("UUID", global,
// false)
+ "importClass(java.util.UUID);\n"
+ "UUID.randomUUID();\n"); // calls getPkgProperty("UUID",
// NativeJavaPackage, true)
assertTrue(Context.jsToJava(result, UUID.class) instanceof UUID);
}

@Test
public void importInSameContext() {
Object result = runScript("importClass(java.util.UUID);UUID.randomUUID();");
assertTrue(Context.jsToJava(result, UUID.class) instanceof UUID);
result = runScript("importClass(java.util.UUID);UUID.randomUUID();");
assertTrue(Context.jsToJava(result, UUID.class) instanceof UUID);
}

@Test
public void importMultipleTimes() {
Utils.runWithAllOptimizationLevels(cx -> {
ScriptableObject sharedScope = cx.initStandardObjects();
//sharedScope.sealObject(); code below will try to modify sealed object

Script script = cx.compileString("importClass(java.util.UUID);true", "TestScript", 1, null);

for (int i = 0; i < 3; i++) {
Scriptable scope = new ImporterTopLevel(cx);
scope.setParentScope(sharedScope);
script.exec(cx, scope);
assertEquals(UniqueTag.NOT_FOUND, sharedScope.get("UUID", sharedScope));
assertTrue(scope.get("UUID", scope) instanceof NativeJavaClass);
}
return null;
});
}

private Object runScript(final String scriptSourceText) {

return contextFactory.call(
context -> {
Script script = context.compileString(scriptSourceText, "", 1, null);
Object exec = script.exec(context, global);
return exec;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.mozilla.javascript.EvaluatorException;
import org.mozilla.javascript.IdFunctionObject;
import org.mozilla.javascript.ImporterTopLevel;
import org.mozilla.javascript.NativeObject;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.Wrapper;

Expand All @@ -40,12 +41,10 @@ public void setUp() throws Exception {
}

ctx = Context.enter();
scope1 = ctx.newObject(sharedScope);
scope1 = new NativeObject();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was done too complex

scope1.setPrototype(sharedScope);
scope1.setParentScope(null);
scope2 = ctx.newObject(sharedScope);
scope2 = new NativeObject();
scope2.setPrototype(sharedScope);
scope2.setParentScope(null);
}

@After
Expand Down
Loading