310 }
311
312 if (value instanceof MethodHandle) {
313 value = ((GlobalObject)global).newScriptFunction(name, (MethodHandle)value, null, false);
314 }
315
316 if (!(value instanceof ScriptFunction)) {
317 typeError(global, "not.a.function", name);
318 }
319
320 if (value instanceof ScriptFunction) {
321 return ScriptObjectMirror.unwrap(ScriptRuntime.apply((ScriptFunction)value, global, args), global);
322 }
323
324 typeError(global, "not.a.function", ScriptRuntime.safeToString(name));
325
326 return UNDEFINED;
327 }
328
329 private void evalEngineScript() throws ScriptException {
330 final URL url = NashornScriptEngine.class.getResource("resources/engine.js");
331 try {
332 final InputStream is = url.openStream();
333 put(ScriptEngine.FILENAME, url);
334 try (final InputStreamReader isr = new InputStreamReader(is)) {
335 eval(isr);
336 }
337 } catch (final IOException e) {
338 throw new ScriptException(e);
339 } finally {
340 put(ScriptEngine.FILENAME, null);
341 }
342 }
343
344 // scripts should see "context" and "engine" as variables
345 private void setContextVariables(final ScriptContext ctxt) {
346 ctxt.setAttribute("context", ctxt, ScriptContext.ENGINE_SCOPE);
347 // current ScriptContext exposed as "context"
348 global.set("context", ctxt, false);
349 Object args = ctxt.getAttribute("arguments");
350 // if no arguments passed, make it empty array
418 }
419 }
420 }
421
422 private Object evalImpl(final char[] buf, final ScriptContext ctxt) throws ScriptException {
423 return evalImpl(compileImpl(buf, ctxt), ctxt);
424 }
425
426 private Object evalImpl(final ScriptFunction script, final ScriptContext ctxt) throws ScriptException {
427 if (script == null) {
428 return null;
429 }
430 final ScriptObject oldGlobal = Context.getGlobal();
431 final boolean globalChanged = (oldGlobal != global);
432 try {
433 if (globalChanged) {
434 setNashornGlobal(global);
435 }
436
437 setContextVariables(ctxt);
438 Object res = ScriptRuntime.apply(script, global);
439 res = ScriptObjectMirror.wrap(res, global);
440 return (res == UNDEFINED) ? null : res;
441 } catch (final Exception e) {
442 throwAsScriptException(e);
443 throw new AssertionError("should not reach here");
444 } finally {
445 if (globalChanged) {
446 setNashornGlobal(oldGlobal);
447 }
448 }
449 }
450
451 private static void throwAsScriptException(final Exception e) throws ScriptException {
452 if (e instanceof ScriptException) {
453 throw (ScriptException)e;
454 } else if (e instanceof NashornException) {
455 final NashornException ne = (NashornException)e;
456 final ScriptException se = new ScriptException(
457 ne.getMessage(), ne.getFileName(),
469 private CompiledScript asCompiledScript(final ScriptFunction script) {
470 return new CompiledScript() {
471 @Override
472 public Object eval(final ScriptContext ctxt) throws ScriptException {
473 return evalImpl(script, ctxt);
474 }
475 @Override
476 public ScriptEngine getEngine() {
477 return NashornScriptEngine.this;
478 }
479 };
480 }
481
482 private ScriptFunction compileImpl(final char[] buf, final ScriptContext ctxt) throws ScriptException {
483 final ScriptObject oldGlobal = Context.getGlobal();
484 final boolean globalChanged = (oldGlobal != global);
485 try {
486 final Object val = ctxt.getAttribute(ScriptEngine.FILENAME);
487 final String fileName = (val != null) ? val.toString() : "<eval>";
488
489 // !!HACK!! do not evaluate "init.js" from jrunscript tool!!
490 if ("<system-init>".equals(fileName)) {
491 return null;
492 }
493
494 final Source source = new Source(fileName, buf);
495 if (globalChanged) {
496 setNashornGlobal(global);
497 }
498
499 setContextVariables(ctxt);
500 return nashornContext.compileScript(source, global, nashornContext._strict);
501 } catch (final Exception e) {
502 throwAsScriptException(e);
503 throw new AssertionError("should not reach here");
504 } finally {
505 if (globalChanged) {
506 setNashornGlobal(oldGlobal);
507 }
508 }
509 }
510
511 // don't make this public!!
512 static void setNashornGlobal(final ScriptObject global) {
513 AccessController.doPrivileged(new PrivilegedAction<Void>() {
|
310 }
311
312 if (value instanceof MethodHandle) {
313 value = ((GlobalObject)global).newScriptFunction(name, (MethodHandle)value, null, false);
314 }
315
316 if (!(value instanceof ScriptFunction)) {
317 typeError(global, "not.a.function", name);
318 }
319
320 if (value instanceof ScriptFunction) {
321 return ScriptObjectMirror.unwrap(ScriptRuntime.apply((ScriptFunction)value, global, args), global);
322 }
323
324 typeError(global, "not.a.function", ScriptRuntime.safeToString(name));
325
326 return UNDEFINED;
327 }
328
329 private void evalEngineScript() throws ScriptException {
330 evalSupportScript("resources/engine.js");
331 }
332
333 private void evalSupportScript(String script) throws ScriptException {
334 final URL url = NashornScriptEngine.class.getResource(script);
335 try {
336 final InputStream is = url.openStream();
337 put(ScriptEngine.FILENAME, url);
338 try (final InputStreamReader isr = new InputStreamReader(is)) {
339 eval(isr);
340 }
341 } catch (final IOException e) {
342 throw new ScriptException(e);
343 } finally {
344 put(ScriptEngine.FILENAME, null);
345 }
346 }
347
348 // scripts should see "context" and "engine" as variables
349 private void setContextVariables(final ScriptContext ctxt) {
350 ctxt.setAttribute("context", ctxt, ScriptContext.ENGINE_SCOPE);
351 // current ScriptContext exposed as "context"
352 global.set("context", ctxt, false);
353 Object args = ctxt.getAttribute("arguments");
354 // if no arguments passed, make it empty array
422 }
423 }
424 }
425
426 private Object evalImpl(final char[] buf, final ScriptContext ctxt) throws ScriptException {
427 return evalImpl(compileImpl(buf, ctxt), ctxt);
428 }
429
430 private Object evalImpl(final ScriptFunction script, final ScriptContext ctxt) throws ScriptException {
431 if (script == null) {
432 return null;
433 }
434 final ScriptObject oldGlobal = Context.getGlobal();
435 final boolean globalChanged = (oldGlobal != global);
436 try {
437 if (globalChanged) {
438 setNashornGlobal(global);
439 }
440
441 setContextVariables(ctxt);
442 final Object val = ctxt.getAttribute(ScriptEngine.FILENAME);
443 final String fileName = (val != null) ? val.toString() : "<eval>";
444
445 // if this is jrunscript's init.js, we want to run the replacement
446 if ("<system-init>".equals(fileName)) {
447 evalSupportScript("resources/init.js");
448 return null;
449 }
450
451 Object res = ScriptRuntime.apply(script, global);
452 res = ScriptObjectMirror.wrap(res, global);
453 return (res == UNDEFINED) ? null : res;
454 } catch (final Exception e) {
455 throwAsScriptException(e);
456 throw new AssertionError("should not reach here");
457 } finally {
458 if (globalChanged) {
459 setNashornGlobal(oldGlobal);
460 }
461 }
462 }
463
464 private static void throwAsScriptException(final Exception e) throws ScriptException {
465 if (e instanceof ScriptException) {
466 throw (ScriptException)e;
467 } else if (e instanceof NashornException) {
468 final NashornException ne = (NashornException)e;
469 final ScriptException se = new ScriptException(
470 ne.getMessage(), ne.getFileName(),
482 private CompiledScript asCompiledScript(final ScriptFunction script) {
483 return new CompiledScript() {
484 @Override
485 public Object eval(final ScriptContext ctxt) throws ScriptException {
486 return evalImpl(script, ctxt);
487 }
488 @Override
489 public ScriptEngine getEngine() {
490 return NashornScriptEngine.this;
491 }
492 };
493 }
494
495 private ScriptFunction compileImpl(final char[] buf, final ScriptContext ctxt) throws ScriptException {
496 final ScriptObject oldGlobal = Context.getGlobal();
497 final boolean globalChanged = (oldGlobal != global);
498 try {
499 final Object val = ctxt.getAttribute(ScriptEngine.FILENAME);
500 final String fileName = (val != null) ? val.toString() : "<eval>";
501
502 final Source source = new Source(fileName, buf);
503 if (globalChanged) {
504 setNashornGlobal(global);
505 }
506
507 setContextVariables(ctxt);
508 return nashornContext.compileScript(source, global, nashornContext._strict);
509 } catch (final Exception e) {
510 throwAsScriptException(e);
511 throw new AssertionError("should not reach here");
512 } finally {
513 if (globalChanged) {
514 setNashornGlobal(oldGlobal);
515 }
516 }
517 }
518
519 // don't make this public!!
520 static void setNashornGlobal(final ScriptObject global) {
521 AccessController.doPrivileged(new PrivilegedAction<Void>() {
|