27
27
import org .elasticsearch .painless .ScriptClassInfo ;
28
28
import org .elasticsearch .painless .WriterConstants ;
29
29
import org .elasticsearch .painless .symbol .ScopeTable ;
30
- import org .elasticsearch .painless .symbol .ScopeTable .Variable ;
31
30
import org .elasticsearch .painless .symbol .ScriptRoot ;
32
31
import org .objectweb .asm .ClassVisitor ;
33
- import org .objectweb .asm .Label ;
34
32
import org .objectweb .asm .Opcodes ;
35
33
import org .objectweb .asm .Type ;
36
- import org .objectweb .asm .commons .Method ;
37
34
import org .objectweb .asm .util .Printer ;
38
35
39
36
import java .lang .invoke .MethodType ;
46
43
47
44
import static org .elasticsearch .painless .WriterConstants .BASE_INTERFACE_TYPE ;
48
45
import static org .elasticsearch .painless .WriterConstants .BITSET_TYPE ;
49
- import static org .elasticsearch .painless .WriterConstants .BOOTSTRAP_METHOD_ERROR_TYPE ;
50
46
import static org .elasticsearch .painless .WriterConstants .CLASS_TYPE ;
51
- import static org .elasticsearch .painless .WriterConstants .COLLECTIONS_TYPE ;
52
- import static org .elasticsearch .painless .WriterConstants .CONVERT_TO_SCRIPT_EXCEPTION_METHOD ;
53
47
import static org .elasticsearch .painless .WriterConstants .DEFINITION_TYPE ;
54
48
import static org .elasticsearch .painless .WriterConstants .DEF_BOOTSTRAP_DELEGATE_METHOD ;
55
49
import static org .elasticsearch .painless .WriterConstants .DEF_BOOTSTRAP_DELEGATE_TYPE ;
56
50
import static org .elasticsearch .painless .WriterConstants .DEF_BOOTSTRAP_METHOD ;
57
- import static org .elasticsearch .painless .WriterConstants .EMPTY_MAP_METHOD ;
58
- import static org .elasticsearch .painless .WriterConstants .EXCEPTION_TYPE ;
59
51
import static org .elasticsearch .painless .WriterConstants .FUNCTION_TABLE_TYPE ;
60
52
import static org .elasticsearch .painless .WriterConstants .GET_NAME_METHOD ;
61
53
import static org .elasticsearch .painless .WriterConstants .GET_SOURCE_METHOD ;
62
54
import static org .elasticsearch .painless .WriterConstants .GET_STATEMENTS_METHOD ;
63
- import static org .elasticsearch .painless .WriterConstants .OUT_OF_MEMORY_ERROR_TYPE ;
64
- import static org .elasticsearch .painless .WriterConstants .PAINLESS_ERROR_TYPE ;
65
- import static org .elasticsearch .painless .WriterConstants .PAINLESS_EXPLAIN_ERROR_GET_HEADERS_METHOD ;
66
- import static org .elasticsearch .painless .WriterConstants .PAINLESS_EXPLAIN_ERROR_TYPE ;
67
- import static org .elasticsearch .painless .WriterConstants .STACK_OVERFLOW_ERROR_TYPE ;
68
55
import static org .elasticsearch .painless .WriterConstants .STRING_TYPE ;
69
56
70
57
public class ClassNode extends IRNode {
@@ -73,7 +60,6 @@ public class ClassNode extends IRNode {
73
60
74
61
private final List <FieldNode > fieldNodes = new ArrayList <>();
75
62
private final List <FunctionNode > functionNodes = new ArrayList <>();
76
- private final List <StatementNode > statementNodes = new ArrayList <>();
77
63
78
64
public void addFieldNode (FieldNode fieldNode ) {
79
65
fieldNodes .add (fieldNode );
@@ -90,14 +76,6 @@ public void addFunctionNode(FunctionNode functionNode) {
90
76
public List <FunctionNode > getFunctionsNodes () {
91
77
return functionNodes ;
92
78
}
93
-
94
- public void addStatementNode (StatementNode statementNode ) {
95
- statementNodes .add (statementNode );
96
- }
97
-
98
- public List <StatementNode > getStatementsNodes () {
99
- return statementNodes ;
100
- }
101
79
102
80
/* ---- end tree structure, begin node data ---- */
103
81
@@ -106,7 +84,6 @@ public List<StatementNode> getStatementsNodes() {
106
84
private String sourceText ;
107
85
private Printer debugStream ;
108
86
private ScriptRoot scriptRoot ;
109
- private boolean doesMethodEscape ;
110
87
111
88
public void setScriptClassInfo (ScriptClassInfo scriptClassInfo ) {
112
89
this .scriptClassInfo = scriptClassInfo ;
@@ -148,14 +125,6 @@ public ScriptRoot getScriptRoot() {
148
125
return scriptRoot ;
149
126
}
150
127
151
- public void setMethodEscape (boolean doesMethodEscape ) {
152
- this .doesMethodEscape = doesMethodEscape ;
153
- }
154
-
155
- public boolean doesMethodEscape () {
156
- return doesMethodEscape ;
157
- }
158
-
159
128
/* ---- end node data ---- */
160
129
161
130
protected Globals globals ;
@@ -245,12 +214,6 @@ public Map<String, Object> write() {
245
214
statementsMethod .returnValue ();
246
215
statementsMethod .endMethod ();
247
216
248
- // Write the method defined in the interface:
249
- MethodWriter executeMethod = classWriter .newMethodWriter (Opcodes .ACC_PUBLIC , scriptClassInfo .getExecuteMethod ());
250
- executeMethod .visitCode ();
251
- write (classWriter , executeMethod , globals , new ScopeTable ());
252
- executeMethod .endMethod ();
253
-
254
217
// Write all fields:
255
218
for (FieldNode fieldNode : fieldNodes ) {
256
219
fieldNode .write (classWriter , null , null , null );
@@ -305,116 +268,4 @@ public Map<String, Object> write() {
305
268
306
269
return statics ;
307
270
}
308
-
309
- @ Override
310
- protected void write (ClassWriter classWriter , MethodWriter methodWriter , Globals globals , ScopeTable scopeTable ) {
311
- // We wrap the whole method in a few try/catches to handle and/or convert other exceptions to ScriptException
312
- Label startTry = new Label ();
313
- Label endTry = new Label ();
314
- Label startExplainCatch = new Label ();
315
- Label startOtherCatch = new Label ();
316
- Label endCatch = new Label ();
317
- methodWriter .mark (startTry );
318
-
319
- scopeTable .defineInternalVariable (Object .class , "this" );
320
-
321
- // Method arguments
322
- for (ScriptClassInfo .MethodArgument arg : scriptClassInfo .getExecuteArguments ()) {
323
- scopeTable .defineVariable (arg .getClazz (), arg .getName ());
324
- }
325
-
326
- if (scriptRoot .getCompilerSettings ().getMaxLoopCounter () > 0 ) {
327
- // if there is infinite loop protection, we do this once:
328
- // int #loop = settings.getMaxLoopCounter()
329
-
330
- Variable loop = scopeTable .defineInternalVariable (int .class , "loop" );
331
-
332
- methodWriter .push (scriptRoot .getCompilerSettings ().getMaxLoopCounter ());
333
- methodWriter .visitVarInsn (Opcodes .ISTORE , loop .getSlot ());
334
- }
335
-
336
- for (int getMethodIndex = 0 ; getMethodIndex < scriptClassInfo .getGetMethods ().size (); ++getMethodIndex ) {
337
- Method method = scriptClassInfo .getGetMethods ().get (getMethodIndex );
338
- Class <?> returnType = scriptClassInfo .getGetReturns ().get (getMethodIndex );
339
-
340
- String name = method .getName ().substring (3 );
341
- name = Character .toLowerCase (name .charAt (0 )) + name .substring (1 );
342
-
343
- if (scriptRoot .getUsedVariables ().contains (name )) {
344
- Variable variable = scopeTable .defineVariable (returnType , name );
345
-
346
- methodWriter .loadThis ();
347
- methodWriter .invokeVirtual (Type .getType (scriptClassInfo .getBaseClass ()), method );
348
- methodWriter .visitVarInsn (method .getReturnType ().getOpcode (Opcodes .ISTORE ), variable .getSlot ());
349
- }
350
- }
351
-
352
- for (StatementNode statementNode : statementNodes ) {
353
- statementNode .write (classWriter , methodWriter , globals , scopeTable );
354
- }
355
-
356
- if (doesMethodEscape == false ) {
357
- switch (scriptClassInfo .getExecuteMethod ().getReturnType ().getSort ()) {
358
- case org .objectweb .asm .Type .VOID :
359
- break ;
360
- case org .objectweb .asm .Type .BOOLEAN :
361
- methodWriter .push (false );
362
- break ;
363
- case org .objectweb .asm .Type .BYTE :
364
- methodWriter .push (0 );
365
- break ;
366
- case org .objectweb .asm .Type .SHORT :
367
- methodWriter .push (0 );
368
- break ;
369
- case org .objectweb .asm .Type .INT :
370
- methodWriter .push (0 );
371
- break ;
372
- case org .objectweb .asm .Type .LONG :
373
- methodWriter .push (0L );
374
- break ;
375
- case org .objectweb .asm .Type .FLOAT :
376
- methodWriter .push (0f );
377
- break ;
378
- case org .objectweb .asm .Type .DOUBLE :
379
- methodWriter .push (0d );
380
- break ;
381
- default :
382
- methodWriter .visitInsn (Opcodes .ACONST_NULL );
383
- }
384
- methodWriter .returnValue ();
385
- }
386
-
387
- methodWriter .mark (endTry );
388
- methodWriter .goTo (endCatch );
389
- // This looks like:
390
- // } catch (PainlessExplainError e) {
391
- // throw this.convertToScriptException(e, e.getHeaders($DEFINITION))
392
- // }
393
- methodWriter .visitTryCatchBlock (startTry , endTry , startExplainCatch , PAINLESS_EXPLAIN_ERROR_TYPE .getInternalName ());
394
- methodWriter .mark (startExplainCatch );
395
- methodWriter .loadThis ();
396
- methodWriter .swap ();
397
- methodWriter .dup ();
398
- methodWriter .getStatic (CLASS_TYPE , "$DEFINITION" , DEFINITION_TYPE );
399
- methodWriter .invokeVirtual (PAINLESS_EXPLAIN_ERROR_TYPE , PAINLESS_EXPLAIN_ERROR_GET_HEADERS_METHOD );
400
- methodWriter .invokeInterface (BASE_INTERFACE_TYPE , CONVERT_TO_SCRIPT_EXCEPTION_METHOD );
401
- methodWriter .throwException ();
402
- // This looks like:
403
- // } catch (PainlessError | BootstrapMethodError | OutOfMemoryError | StackOverflowError | Exception e) {
404
- // throw this.convertToScriptException(e, e.getHeaders())
405
- // }
406
- // We *think* it is ok to catch OutOfMemoryError and StackOverflowError because Painless is stateless
407
- methodWriter .visitTryCatchBlock (startTry , endTry , startOtherCatch , PAINLESS_ERROR_TYPE .getInternalName ());
408
- methodWriter .visitTryCatchBlock (startTry , endTry , startOtherCatch , BOOTSTRAP_METHOD_ERROR_TYPE .getInternalName ());
409
- methodWriter .visitTryCatchBlock (startTry , endTry , startOtherCatch , OUT_OF_MEMORY_ERROR_TYPE .getInternalName ());
410
- methodWriter .visitTryCatchBlock (startTry , endTry , startOtherCatch , STACK_OVERFLOW_ERROR_TYPE .getInternalName ());
411
- methodWriter .visitTryCatchBlock (startTry , endTry , startOtherCatch , EXCEPTION_TYPE .getInternalName ());
412
- methodWriter .mark (startOtherCatch );
413
- methodWriter .loadThis ();
414
- methodWriter .swap ();
415
- methodWriter .invokeStatic (COLLECTIONS_TYPE , EMPTY_MAP_METHOD );
416
- methodWriter .invokeInterface (BASE_INTERFACE_TYPE , CONVERT_TO_SCRIPT_EXCEPTION_METHOD );
417
- methodWriter .throwException ();
418
- methodWriter .mark (endCatch );
419
- }
420
271
}
0 commit comments