Skip to content
Open
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
2 changes: 1 addition & 1 deletion boot-script/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<source>19</source>
<target>1.8</target>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package net.java.html.boot.script;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.lang.ref.WeakReference;
Expand All @@ -28,6 +29,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.Executor;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand All @@ -37,6 +39,7 @@
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.script.SimpleScriptContext;
import net.java.html.boot.script.impl.Callback;
import net.java.html.boot.script.impl.PromisePolyfill;
import org.netbeans.html.boot.spi.Fn;
Expand Down Expand Up @@ -157,7 +160,18 @@ public void displayPage(URL page, Runnable onPageLoad) {

@Override
public void loadScript(Reader code) throws Exception {
eng.eval(code);
var ctx = eng.getContext();
if (code instanceof Callable<?> moreInfo && moreInfo.call() instanceof URL u) {
try {
var file = new File(u.toURI());
if (file.exists()) {
ctx.setAttribute(ScriptEngine.FILENAME, file.getPath(), ScriptContext.ENGINE_SCOPE);
}
} catch (IllegalArgumentException ex) {
// ignore URLs not representing a file
}
}
eng.eval(code, ctx);
}

//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.lang.reflect.Method;
import net.java.html.js.JavaScriptBody;
import org.netbeans.html.boot.spi.Fn;
import org.netbeans.html.boot.impl.FnContext;
import org.testng.IHookCallBack;
import org.testng.IHookable;
import org.testng.ITest;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package net.java.html.boot.script;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.regex.Pattern;
import net.java.html.js.JavaScriptBody;
import net.java.html.js.JavaScriptResource;
import org.netbeans.html.boot.spi.Fn;
import org.testng.Assert;
import org.testng.annotations.Test;

@JavaScriptResource("throw.js")
public final class ScriptEngineEvalTest {
public ScriptEngineEvalTest() {
}

@Test
public void fileNameIsDefinedWhenEvaluatingAResourceFile() throws Exception {
try (var p = Fn.activate(Scripts.newPresenter().build())) {
yieldError("Everything is OK!");
} catch (Exception ex) {
assertStackContains(ex,
"Caused by.*Exception: Everything is OK!",
"at <js>.yieldError.*throw.js:[0-9]+",
"at <js>.*anonymous.*eval.*[0-9]+"
);
}
}

@JavaScriptBody(args = { "msg" }, body = """
yieldError(msg);
""")
private static void yieldError(String msg) {
throw new AssertionError(msg);
}

private void assertStackContains(Exception ex, String... patterns) {
var w = new StringWriter();
ex.printStackTrace(new PrintWriter(w));
var lines = w.toString().split("\n");

FOUND: for (var p : patterns) {
var c = Pattern.compile(p);
for (var l : lines) {
if (c.matcher(l).find()) {
continue FOUND;
}
}
Assert.fail("Cannot find " + p + " in:\n" + w);
}
}

}
22 changes: 22 additions & 0 deletions boot-script/src/test/resources/net/java/html/boot/script/throw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

function yieldError(msg) {
throw msg;
}
Loading
Loading