@@ -366,44 +366,7 @@ private void writeNeedsMethods(Class<?> clazz, ClassWriter writer, MainMethodRes
366366 }
367367
368368 Object compile (Compiler compiler , String scriptName , String source , Map <String , String > params , Object ... args ) {
369- final CompilerSettings compilerSettings ;
370-
371- if (params .isEmpty ()) {
372- // Use the default settings.
373- compilerSettings = defaultCompilerSettings ;
374- } else {
375- // Use custom settings specified by params.
376- compilerSettings = new CompilerSettings ();
377-
378- // Except regexes enabled - this is a node level setting and can't be changed in the request.
379- compilerSettings .setRegexesEnabled (defaultCompilerSettings .areRegexesEnabled ());
380-
381- Map <String , String > copy = new HashMap <>(params );
382-
383- String value = copy .remove (CompilerSettings .MAX_LOOP_COUNTER );
384- if (value != null ) {
385- compilerSettings .setMaxLoopCounter (Integer .parseInt (value ));
386- }
387-
388- value = copy .remove (CompilerSettings .PICKY );
389- if (value != null ) {
390- compilerSettings .setPicky (Boolean .parseBoolean (value ));
391- }
392-
393- value = copy .remove (CompilerSettings .INITIAL_CALL_SITE_DEPTH );
394- if (value != null ) {
395- compilerSettings .setInitialCallSiteDepth (Integer .parseInt (value ));
396- }
397-
398- value = copy .remove (CompilerSettings .REGEX_ENABLED .getKey ());
399- if (value != null ) {
400- throw new IllegalArgumentException ("[painless.regex.enabled] can only be set on node startup." );
401- }
402-
403- if (!copy .isEmpty ()) {
404- throw new IllegalArgumentException ("Unrecognized compile-time parameter(s): " + copy );
405- }
406- }
369+ final CompilerSettings compilerSettings = buildCompilerSettings (params );
407370
408371 // Check we ourselves are not being called by unprivileged code.
409372 SpecialPermission .check ();
@@ -434,14 +397,33 @@ public Object run() {
434397 }, COMPILATION_CONTEXT );
435398 // Note that it is safe to catch any of the following errors since Painless is stateless.
436399 } catch (OutOfMemoryError | StackOverflowError | VerifyError | Exception e ) {
437- throw convertToScriptException (scriptName == null ? source : scriptName , source , e );
400+ throw convertToScriptException (source , e );
438401 }
439402 }
440403
441404 void compile (Compiler compiler , Loader loader , MainMethodReserved reserved ,
442405 String scriptName , String source , Map <String , String > params ) {
443- final CompilerSettings compilerSettings ;
406+ final CompilerSettings compilerSettings = buildCompilerSettings (params );
407+
408+ try {
409+ // Drop all permissions to actually compile the code itself.
410+ AccessController .doPrivileged (new PrivilegedAction <Void >() {
411+ @ Override
412+ public Void run () {
413+ String name = scriptName == null ? source : scriptName ;
414+ compiler .compile (loader , reserved , name , source , compilerSettings );
415+
416+ return null ;
417+ }
418+ }, COMPILATION_CONTEXT );
419+ // Note that it is safe to catch any of the following errors since Painless is stateless.
420+ } catch (OutOfMemoryError | StackOverflowError | VerifyError | Exception e ) {
421+ throw convertToScriptException (source , e );
422+ }
423+ }
444424
425+ private CompilerSettings buildCompilerSettings (Map <String , String > params ) {
426+ CompilerSettings compilerSettings ;
445427 if (params .isEmpty ()) {
446428 // Use the default settings.
447429 compilerSettings = defaultCompilerSettings ;
@@ -478,25 +460,10 @@ void compile(Compiler compiler, Loader loader, MainMethodReserved reserved,
478460 throw new IllegalArgumentException ("Unrecognized compile-time parameter(s): " + copy );
479461 }
480462 }
481-
482- try {
483- // Drop all permissions to actually compile the code itself.
484- AccessController .doPrivileged (new PrivilegedAction <Void >() {
485- @ Override
486- public Void run () {
487- String name = scriptName == null ? source : scriptName ;
488- compiler .compile (loader , reserved , name , source , compilerSettings );
489-
490- return null ;
491- }
492- }, COMPILATION_CONTEXT );
493- // Note that it is safe to catch any of the following errors since Painless is stateless.
494- } catch (OutOfMemoryError | StackOverflowError | VerifyError | Exception e ) {
495- throw convertToScriptException (scriptName == null ? source : scriptName , source , e );
496- }
463+ return compilerSettings ;
497464 }
498465
499- private ScriptException convertToScriptException (String scriptName , String scriptSource , Throwable t ) {
466+ private ScriptException convertToScriptException (String scriptSource , Throwable t ) {
500467 // create a script stack: this is just the script portion
501468 List <String > scriptStack = new ArrayList <>();
502469 for (StackTraceElement element : t .getStackTrace ()) {
@@ -507,7 +474,7 @@ private ScriptException convertToScriptException(String scriptName, String scrip
507474 scriptStack .add ("<<< unknown portion of script >>>" );
508475 } else {
509476 offset --; // offset is 1 based, line numbers must be!
510- int startOffset = getPreviousStatement (scriptSource , offset );
477+ int startOffset = getPreviousStatement (offset );
511478 int endOffset = getNextStatement (scriptSource , offset );
512479 StringBuilder snippet = new StringBuilder ();
513480 if (startOffset > 0 ) {
@@ -535,7 +502,7 @@ private ScriptException convertToScriptException(String scriptName, String scrip
535502 }
536503
537504 // very simple heuristic: +/- 25 chars. can be improved later.
538- private int getPreviousStatement (String scriptSource , int offset ) {
505+ private int getPreviousStatement (int offset ) {
539506 return Math .max (0 , offset - 25 );
540507 }
541508
0 commit comments