1 package io.jawk.backend;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import java.io.Closeable;
26 import java.io.IOException;
27 import java.io.PrintStream;
28 import java.util.AbstractSet;
29 import java.util.Iterator;
30 import java.util.Arrays;
31 import java.util.HashSet;
32 import java.util.LinkedHashMap;
33 import java.util.LinkedHashSet;
34 import java.util.Objects;
35 import java.util.ArrayDeque;
36 import java.util.ArrayList;
37 import java.util.Collections;
38 import java.util.Deque;
39 import java.util.Enumeration;
40 import java.util.HashMap;
41 import java.util.IdentityHashMap;
42 import java.util.List;
43 import java.util.Locale;
44 import java.util.Map;
45 import java.util.Set;
46 import java.util.function.BiConsumer;
47 import java.util.regex.Pattern;
48 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
49 import io.jawk.AwkExpression;
50 import io.jawk.AwkProgram;
51 import io.jawk.AwkSandboxException;
52 import io.jawk.ExitException;
53 import io.jawk.ext.AbstractExtension;
54 import io.jawk.ext.ExtensionFunction;
55 import io.jawk.ext.ForInKeyOrder;
56 import io.jawk.ext.JawkExtension;
57 import io.jawk.intermediate.Address;
58 import io.jawk.intermediate.BuiltinFunction;
59 import io.jawk.intermediate.Opcode;
60 import io.jawk.intermediate.PositionTracker;
61 import io.jawk.intermediate.Tuple;
62 import io.jawk.intermediate.Tuple.BooleanTuple;
63 import io.jawk.intermediate.Tuple.CallFunctionTuple;
64 import io.jawk.intermediate.Tuple.ClassTuple;
65 import io.jawk.intermediate.Tuple.CountAndAppendTuple;
66 import io.jawk.intermediate.Tuple.CountTuple;
67 import io.jawk.intermediate.Tuple.DereferenceTuple;
68 import io.jawk.intermediate.Tuple.ExtensionTuple;
69 import io.jawk.intermediate.Tuple.IndirectCallTuple;
70 import io.jawk.intermediate.Tuple.IndirectFunctionTarget;
71 import io.jawk.intermediate.Tuple.InputFieldTuple;
72 import io.jawk.intermediate.Tuple.LongTuple;
73 import io.jawk.intermediate.Tuple.PushDoubleTuple;
74 import io.jawk.intermediate.Tuple.PushLongTuple;
75 import io.jawk.intermediate.Tuple.PushStringTuple;
76 import io.jawk.intermediate.Tuple.RegexTuple;
77 import io.jawk.intermediate.Tuple.ScalarPopTuple;
78 import io.jawk.intermediate.Tuple.SubstitutionVariableTuple;
79 import io.jawk.intermediate.Tuple.VariableTuple;
80 import io.jawk.intermediate.UninitializedObject;
81 import io.jawk.intermediate.UntypedObject;
82 import io.jawk.jrt.AssocArray;
83 import io.jawk.jrt.AwkRuntimeException;
84 import io.jawk.jrt.AwkSink;
85 import io.jawk.jrt.ConditionPair;
86 import io.jawk.jrt.InputSource;
87 import io.jawk.jrt.JRT;
88 import io.jawk.jrt.VariableManager;
89 import io.jawk.util.AwkSettings;
90 import io.jawk.jrt.BSDRandom;
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124 public class AVM implements VariableManager, Closeable {
125
126 private RuntimeStack runtimeStack = new RuntimeStack();
127
128
129
130
131 private Object[] operandStack = new Object[INITIAL_OPERAND_STACK_CAPACITY];
132 private int operandStackSize;
133
134
135
136
137 private final Deque<IndirectArrayArgumentReference> elementArgumentReferences = new ArrayDeque<IndirectArrayArgumentReference>();
138 private List<String> arguments;
139 private boolean sortedArrayKeys;
140 private final Map<String, Object> baseInitialVariables;
141 private final Map<String, Object> baseSpecialVariables;
142 private Map<String, Object> executionInitialVariables;
143 private Map<String, Object> executionSpecialVariables;
144 private JRT jrt;
145 private Map<String, JawkExtension> extensionInstances;
146
147 private static final int INITIAL_OPERAND_STACK_CAPACITY = 64;
148
149
150 private Object pop() {
151 Object value = operandStack[--operandStackSize];
152 operandStack[operandStackSize] = null;
153 return value;
154 }
155
156 private void push(Object o) {
157 if (operandStackSize == operandStack.length) {
158 operandStack = Arrays.copyOf(operandStack, operandStack.length * 2);
159 }
160 operandStack[operandStackSize++] = o;
161 }
162
163 private void clearOperandStack() {
164 Arrays.fill(operandStack, 0, operandStackSize, null);
165 operandStackSize = 0;
166 }
167
168 private final AwkSettings settings;
169 private final boolean profiling;
170 private final Map<Opcode, ProfilingReport.Accumulator> tupleProfilingStats;
171 private final Map<String, ProfilingReport.Accumulator> functionProfilingStats;
172 private final Deque<ActiveFunction> activeProfilingFunctions;
173 private InputSource resolvedInputSource;
174 private AwkExpression installedEvalExpression;
175 private boolean mergedGlobalLayoutActive;
176
177
178 private ForInKeyOrder forInKeyOrder;
179
180
181 private String sourceDescription;
182
183
184 private int currentLineNumber;
185
186
187 private boolean beforeStartHooksExecuted;
188
189
190 private long symtabOffset = NULL_OFFSET;
191
192
193
194
195
196
197
198 public AVM() {
199 this(null, Collections.<String, JawkExtension>emptyMap());
200 }
201
202
203
204
205
206
207
208
209
210 public AVM(final AwkSettings parameters, final Map<String, JawkExtension> extensionInstances) {
211 this(parameters, extensionInstances, false);
212 }
213
214
215
216
217
218
219
220
221
222 public AVM(final AwkSettings parameters, final Map<String, JawkExtension> extensionInstances,
223 final boolean profilingEnabled) {
224 this.settings = parameters != null ? parameters : AwkSettings.DEFAULT_SETTINGS;
225 this.extensionInstances = extensionInstances == null ?
226 Collections.<String, JawkExtension>emptyMap() :
227 extensionInstances;
228 this.profiling = profilingEnabled;
229 if (profilingEnabled) {
230 this.tupleProfilingStats = new java.util.EnumMap<Opcode, ProfilingReport.Accumulator>(Opcode.class);
231 this.functionProfilingStats = new LinkedHashMap<String, ProfilingReport.Accumulator>();
232 this.activeProfilingFunctions = new ArrayDeque<ActiveFunction>();
233 } else {
234 this.tupleProfilingStats = null;
235 this.functionProfilingStats = null;
236 this.activeProfilingFunctions = null;
237 }
238
239 arguments = Collections.emptyList();
240 sortedArrayKeys = this.settings.isUseSortedArrayKeys();
241 baseInitialVariables = new HashMap<String, Object>(this.settings.getVariables());
242 baseSpecialVariables = JRT.copySpecialVariables(baseInitialVariables);
243 executionInitialVariables = baseInitialVariables;
244 executionSpecialVariables = baseSpecialVariables;
245
246 jrt = createJrt();
247 initExtensions();
248 }
249
250
251
252
253
254
255
256
257
258
259
260
261
262 protected JRT createJrt() {
263 return new JRT(this, this.settings.getLocale(), AwkSink.NOP_SINK, null);
264 }
265
266
267
268
269
270
271 protected AwkSettings getSettings() {
272 return settings;
273 }
274
275
276
277
278
279
280 @SuppressFBWarnings("EI_EXPOSE_REP")
281 public JRT getJrt() {
282 return jrt;
283 }
284
285
286
287
288
289
290
291 public void setAwkSink(AwkSink sink) {
292 jrt.setAwkSink(Objects.requireNonNull(sink, "sink"));
293 }
294
295
296
297
298
299
300
301 public void setErrorStream(PrintStream errorStream) {
302 jrt.setErrorStream(errorStream);
303 }
304
305
306
307
308
309
310
311 public void setWarningStream(PrintStream warningStream) {
312 jrt.setWarningStream(warningStream);
313 }
314
315
316
317
318
319
320
321
322
323
324
325
326
327 public void setForInKeyOrder(ForInKeyOrder keyOrder) {
328 forInKeyOrder = keyOrder;
329 }
330
331
332
333
334
335
336 public AwkSink getAwkSink() {
337 return jrt.getAwkSink();
338 }
339
340
341
342
343
344
345 protected Locale getLocale() {
346 return jrt.getLocale();
347 }
348
349
350
351
352
353
354
355
356
357 public Object eval(AwkExpression expression) throws IOException {
358 AwkExpression compiledExpression = Objects.requireNonNull(expression, "expression");
359 installExpressionMetadata(compiledExpression);
360 jrt.setPosix(compiledExpression.isPosix());
361
362 try {
363 executeTuples(compiledExpression.top());
364 } catch (ExitException e) {
365
366
367 throwExitException = false;
368 exitCode = 0;
369 throw new IllegalStateException("eval(AwkExpression) cannot execute EXIT opcodes.", e);
370 }
371 return operandStackSize == 0 ? null : JRT.toJavaScalar(pop());
372 }
373
374
375
376
377
378
379
380
381
382 public Object eval(AwkExpression expression, InputSource inputSource) throws IOException {
383 return eval(expression, inputSource, null);
384 }
385
386
387
388
389
390
391
392
393
394
395
396
397 public Object eval(AwkExpression expression, InputSource inputSource, Map<String, Object> variableOverrides)
398 throws IOException {
399 prepareForEval(inputSource, Collections.<String>emptyList(), variableOverrides);
400 return eval(expression);
401 }
402
403
404
405
406
407
408
409
410
411 public void execute(AwkProgram program, InputSource inputSource) throws ExitException, IOException {
412 execute(program, inputSource, Collections.<String>emptyList(), null);
413 }
414
415
416
417
418
419
420
421
422
423
424 public void execute(AwkProgram program, InputSource inputSource, List<String> runtimeArguments)
425 throws ExitException,
426 IOException {
427 execute(program, inputSource, runtimeArguments, null);
428 }
429
430
431
432
433
434
435
436
437
438
439
440
441
442 public void execute(
443 AwkProgram program,
444 InputSource inputSource,
445 List<String> runtimeArguments,
446 Map<String, Object> variableOverrides)
447 throws ExitException,
448 IOException {
449 AwkProgram compiledProgram = Objects.requireNonNull(program, "program");
450 InputSource resolvedSource = Objects.requireNonNull(inputSource, "inputSource");
451 resetRuntimeState(runtimeArguments, variableOverrides);
452 installProgramMetadata(compiledProgram);
453
454 jrt.setPosix(compiledProgram.isPosix());
455 jrt.prepareForExecution(settings.getFieldSeparator(), settings.getDefaultRS());
456 if (!executionSpecialVariables.isEmpty()) {
457 jrt.applySpecialVariables(executionSpecialVariables);
458 }
459 rebindResolvedInputSource(resolvedSource);
460 executeTuples(compiledProgram.top());
461 }
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476 public void executePersistingGlobals(AwkProgram program, InputSource inputSource) throws ExitException, IOException {
477 executePersistingGlobals(program, inputSource, Collections.<String>emptyList(), null);
478 }
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494 public void executePersistingGlobals(AwkProgram program, InputSource inputSource, List<String> runtimeArguments)
495 throws ExitException,
496 IOException {
497 executePersistingGlobals(program, inputSource, runtimeArguments, null);
498 }
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516 public void executePersistingGlobals(
517 AwkProgram program,
518 InputSource inputSource,
519 List<String> runtimeArguments,
520 Map<String, Object> variableOverrides)
521 throws ExitException,
522 IOException {
523 AwkProgram compiledProgram = Objects.requireNonNull(program, "program");
524 InputSource resolvedSource = Objects.requireNonNull(inputSource, "inputSource");
525 mergeRuntimeState(runtimeArguments, variableOverrides, compiledProgram);
526
527 jrt.setPosix(compiledProgram.isPosix());
528 jrt.prepareForExecution(settings.getFieldSeparator(), settings.getDefaultRS());
529 if (!executionSpecialVariables.isEmpty()) {
530 jrt.applySpecialVariables(executionSpecialVariables);
531 }
532 rebindResolvedInputSource(resolvedSource);
533 executeTuples(compiledProgram.top());
534 }
535
536
537
538
539
540
541
542 public void clearPersistentGlobals() {
543 runtimeStack.clearGlobals();
544 mergedGlobalLayoutActive = false;
545 }
546
547
548
549
550
551
552
553
554
555
556 public Map<String, Object> snapshotPersistentMemory() {
557 return new LinkedHashMap<>(collectPersistentGlobalValues());
558 }
559
560
561
562
563
564
565
566
567
568
569
570 public void restorePersistentMemory(Map<String, Object> snapshot) {
571 Map<String, Object> restoredSnapshot = Objects.requireNonNull(snapshot, "snapshot");
572 Map<String, Object> restoredGlobals = filterToPersistentEligible(restoredSnapshot);
573 runtimeStack.clearGlobals();
574 if (!restoredGlobals.isEmpty()) {
575 runtimeStack.rebindGlobals(new ArrayList<>(restoredGlobals.keySet()));
576 applyGlobalsToStack(restoredGlobals);
577 }
578 mergedGlobalLayoutActive = false;
579 }
580
581 private void initExtensions() {
582 if (extensionInstances.isEmpty()) {
583 return;
584 }
585 Set<JawkExtension> initialized = new LinkedHashSet<JawkExtension>();
586 for (JawkExtension extension : extensionInstances.values()) {
587 if (initialized.add(extension)) {
588 extension.init(this, jrt, settings);
589 }
590 }
591 }
592
593
594
595 private long environOffset = NULL_OFFSET;
596 private long argcOffset = NULL_OFFSET;
597 private long argvOffset = NULL_OFFSET;
598
599 private static final Integer ZERO = Integer.valueOf(0);
600 private static final Integer ONE = Integer.valueOf(1);
601
602
603 private final BSDRandom randomNumberGenerator = new BSDRandom(1);
604
605
606
607
608
609 private Address exitAddress = null;
610
611
612
613
614
615 private Address endFileAddress = null;
616
617
618
619
620
621
622
623 private Address nextFileAddress = null;
624
625
626
627
628
629
630 private Address nextAddress = null;
631
632
633
634
635
636 private boolean withinBeginFileBlocks = false;
637
638
639
640
641
642 private boolean withinEndFileBlocks = false;
643
644
645
646
647
648 private boolean inputFileLoopStarted = false;
649
650
651
652
653
654
655 private boolean mainInputLoopStarted = false;
656
657
658
659
660
661 private boolean withinEndBlocks = false;
662
663
664
665
666 private int exitCode = 0;
667
668
669
670
671 private boolean throwExitException = false;
672
673
674
675
676
677
678 private Map<String, Integer> globalVariableOffsets;
679
680
681
682
683 private Map<String, Boolean> globalVariableArrays;
684 private Set<String> functionNames = Collections.emptySet();
685 private Map<String, Integer> initializedEvalGlobalVariableOffsets;
686 private Map<String, Boolean> initializedEvalGlobalVariableArrays;
687
688
689
690
691
692
693
694
695
696
697 public boolean prepareForEval(String input) throws IOException {
698 return prepareForEval(new SingleRecordInputSource(input), Collections.<String>emptyList(), null);
699 }
700
701
702
703
704
705
706
707
708
709
710
711 public boolean prepareForEval(InputSource inputSource) throws IOException {
712 return prepareForEval(inputSource, Collections.<String>emptyList(), null);
713 }
714
715 private boolean prepareForEval(
716 InputSource inputSource,
717 List<String> runtimeArguments,
718 Map<String, Object> variableOverrides)
719 throws IOException {
720 InputSource resolvedSource = Objects.requireNonNull(inputSource, "inputSource");
721 resetRuntimeState(runtimeArguments, variableOverrides);
722 rebindResolvedInputSource(resolvedSource);
723
724 jrt.jrtCloseAll();
725 jrt.prepareForExecution(settings.getFieldSeparator(), settings.getDefaultRS());
726 if (!executionSpecialVariables.isEmpty()) {
727 jrt.applySpecialVariables(executionSpecialVariables);
728 }
729 return jrt.consumeInputForEval(resolvedInputSource);
730 }
731
732 private void resetRuntimeState(List<String> runtimeArguments, Map<String, Object> variableOverrides) {
733 resetTransientRuntimeState(runtimeArguments, variableOverrides);
734 runtimeStack.clearGlobals();
735 }
736
737 private void resetTransientRuntimeState(List<String> runtimeArguments, Map<String, Object> variableOverrides) {
738
739 clearOperandStack();
740 elementArgumentReferences.clear();
741 environOffset = NULL_OFFSET;
742 argcOffset = NULL_OFFSET;
743 argvOffset = NULL_OFFSET;
744 symtabOffset = NULL_OFFSET;
745 exitAddress = null;
746 endFileAddress = null;
747 nextFileAddress = null;
748 nextAddress = null;
749 withinBeginFileBlocks = false;
750 withinEndFileBlocks = false;
751 inputFileLoopStarted = false;
752 mainInputLoopStarted = false;
753 withinEndBlocks = false;
754 exitCode = 0;
755 throwExitException = false;
756 globalVariableOffsets = null;
757 globalVariableArrays = null;
758 functionNames = Collections.emptySet();
759 initializedEvalGlobalVariableOffsets = null;
760 initializedEvalGlobalVariableArrays = null;
761 installedEvalExpression = null;
762 mergedGlobalLayoutActive = false;
763 runtimeStack.resetTransientState();
764 randomNumberGenerator.setSeed(1);
765
766 prepareExecutionInputs(runtimeArguments, variableOverrides);
767 }
768
769 private void installExpressionMetadata(AwkExpression compiledExpression) {
770 if (installedEvalExpression == compiledExpression) {
771 return;
772 }
773 globalVariableOffsets = compiledExpression.getGlobalVariableOffsetMap();
774 globalVariableArrays = compiledExpression.getGlobalVariableAarrayMap();
775 functionNames = compiledExpression.getFunctionNameSet();
776 installedEvalExpression = compiledExpression;
777 }
778
779 private void installProgramMetadata(AwkProgram compiledProgram) {
780 globalVariableOffsets = compiledProgram.getGlobalVariableOffsetMap();
781 globalVariableArrays = compiledProgram.getGlobalVariableAarrayMap();
782 functionNames = compiledProgram.getFunctionNameSet();
783 sourceDescription = compiledProgram.getSourceDescription();
784 exitAddress = compiledProgram.getExitAddress();
785 endFileAddress = compiledProgram.getEndFileAddress();
786 nextFileAddress = compiledProgram.getNextFileAddress();
787 nextAddress = compiledProgram.getNextAddress();
788 }
789
790 private void rebindResolvedInputSource(InputSource resolvedSource) {
791 InputSource previousResolvedSource = resolvedInputSource;
792 if (previousResolvedSource != null && previousResolvedSource != resolvedSource) {
793 closeInputSource(previousResolvedSource);
794 }
795 resolvedInputSource = resolvedSource;
796
797
798 jrt.bindStandardInput(resolvedSource);
799 }
800
801 private boolean hasCompatibleEvalGlobalLayout(long numGlobals) {
802 Object[] globals = runtimeStack.getNumGlobals();
803 return globals != null
804 && globals.length == numGlobals
805 && Objects.equals(initializedEvalGlobalVariableOffsets, globalVariableOffsets)
806 && Objects.equals(initializedEvalGlobalVariableArrays, globalVariableArrays);
807 }
808
809
810
811
812
813
814
815
816
817
818 private void mergeRuntimeState(
819 List<String> runtimeArguments,
820 Map<String, Object> variableOverrides,
821 AwkProgram compiledProgram) {
822 Map<String, Object> carriedGlobals = collectPersistentGlobalValues();
823 resetTransientRuntimeState(runtimeArguments, variableOverrides);
824 installProgramMetadata(compiledProgram);
825
826 Map<String, Object> basePersistentSeeds = collectBasePersistentGlobalSeeds();
827 Map<String, Object> executionUserSeeds = collectExecutionUserGlobalSeeds(variableOverrides);
828 List<String> mergedGlobalNamesByOffset = buildMergedGlobalNamesByOffset(
829 carriedGlobals,
830 basePersistentSeeds,
831 executionUserSeeds);
832
833 runtimeStack.rebindGlobals(mergedGlobalNamesByOffset);
834 applyGlobalsToStack(carriedGlobals);
835 applyGlobalsToStack(basePersistentSeeds);
836 applyGlobalsToStack(executionUserSeeds);
837 mergedGlobalLayoutActive = true;
838 }
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853 private boolean hasCompatiblePersistentGlobalLayout(long numGlobals) {
854 Object[] globals = runtimeStack.getNumGlobals();
855 if (!mergedGlobalLayoutActive || globals == null || globalVariableOffsets == null || globals.length < numGlobals) {
856 return false;
857 }
858 for (Map.Entry<String, Integer> entry : globalVariableOffsets.entrySet()) {
859 int offset = entry.getValue().intValue();
860 if (offset < 0 || offset >= globals.length || !entry.getKey().equals(runtimeStack.getGlobalName(offset))) {
861 return false;
862 }
863 }
864 return true;
865 }
866
867
868
869
870
871
872
873
874
875
876
877
878 private void applyExecutionInitialVariablesToGlobalSlots(boolean skipPersistentEligibleGlobals) {
879 for (Map.Entry<String, Object> entry : executionInitialVariables.entrySet()) {
880 String key = entry.getKey();
881 if (skipPersistentEligibleGlobals && isPersistentEligibleGlobal(key)) {
882 continue;
883 }
884 if (functionNames.contains(key)) {
885 throw new IllegalArgumentException("Cannot assign a scalar to a function name (" + key + ").");
886 }
887 Integer offsetObj = globalVariableOffsets.get(key);
888 Boolean arrayObj = globalVariableArrays.get(key);
889 if (offsetObj != null) {
890 Object obj = normalizeExternalVariableValue(entry.getValue());
891 if (arrayObj.booleanValue()) {
892 if (obj instanceof Map) {
893 runtimeStack.setFilelistVariable(offsetObj.intValue(), obj);
894 } else {
895 throw new IllegalArgumentException("Cannot assign a scalar to a non-scalar variable (" + key + ").");
896 }
897 } else {
898 runtimeStack.setFilelistVariable(offsetObj.intValue(), obj);
899 }
900 }
901 }
902 }
903
904
905
906
907
908
909
910
911
912
913 private void prepareExecutionInputs(List<String> runtimeArguments, Map<String, Object> variableOverrides) {
914 this.arguments = runtimeArguments != null ? new ArrayList<>(runtimeArguments) : Collections.<String>emptyList();
915
916 if (variableOverrides == null || variableOverrides.isEmpty()) {
917 executionInitialVariables = baseInitialVariables;
918 executionSpecialVariables = baseSpecialVariables;
919 } else {
920 executionInitialVariables = new HashMap<>(baseInitialVariables);
921
922
923
924 for (Map.Entry<String, Object> entry : variableOverrides.entrySet()) {
925 executionInitialVariables.put(entry.getKey(), normalizeExternalVariableValue(entry.getValue()));
926 }
927
928 Map<String, Object> specialOverrides = JRT.copySpecialVariables(variableOverrides);
929 if (specialOverrides.isEmpty()) {
930 executionSpecialVariables = baseSpecialVariables;
931 } else {
932 executionSpecialVariables = new HashMap<>(baseSpecialVariables);
933 executionSpecialVariables.putAll(specialOverrides);
934 }
935 }
936 }
937
938
939
940
941
942
943
944
945 private Map<String, Object> filterToPersistentEligible(Map<String, Object> source) {
946 Map<String, Object> result = new LinkedHashMap<>();
947 for (Map.Entry<String, Object> entry : source.entrySet()) {
948 if (isPersistentEligibleGlobal(entry.getKey())) {
949 result.put(entry.getKey(), entry.getValue());
950 }
951 }
952 return result;
953 }
954
955
956
957
958
959
960 private Map<String, Object> collectPersistentGlobalValues() {
961 return filterToPersistentEligible(runtimeStack.snapshotGlobalVariables());
962 }
963
964
965
966
967
968
969
970 private Map<String, Object> collectBasePersistentGlobalSeeds() {
971 Map<String, Object> basePersistentSeeds = new LinkedHashMap<>();
972 for (Map.Entry<String, Object> entry : baseInitialVariables.entrySet()) {
973 String name = entry.getKey();
974 if (isPersistentEligibleGlobal(name)) {
975 validateSeededGlobalName(name);
976 Object value = normalizeExternalVariableValue(entry.getValue());
977 validateSeededGlobalValue(name, value);
978 basePersistentSeeds.put(name, value);
979 }
980 }
981 return basePersistentSeeds;
982 }
983
984
985
986
987
988
989
990
991
992
993
994 private Map<String, Object> collectExecutionUserGlobalSeeds(Map<String, Object> variableOverrides) {
995 Map<String, Object> executionUserSeeds = new LinkedHashMap<>();
996 if (variableOverrides != null) {
997 for (Map.Entry<String, Object> entry : variableOverrides.entrySet()) {
998 String name = entry.getKey();
999 if (isPersistentEligibleGlobal(name)) {
1000 validateSeededGlobalName(name);
1001 Object value = normalizeExternalVariableValue(entry.getValue());
1002 validateSeededGlobalValue(name, value);
1003 executionUserSeeds.put(name, value);
1004 }
1005 }
1006 }
1007 return executionUserSeeds;
1008 }
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023 private List<String> buildMergedGlobalNamesByOffset(
1024 Map<String, Object> carriedGlobals,
1025 Map<String, Object> basePersistentSeeds,
1026 Map<String, Object> executionUserSeeds) {
1027 LinkedHashSet<String> orderedNames = new LinkedHashSet<>();
1028 List<Map.Entry<String, Integer>> compiledGlobals = new ArrayList<>(globalVariableOffsets.entrySet());
1029 compiledGlobals.sort(java.util.Comparator.comparingInt(Map.Entry::getValue));
1030 for (Map.Entry<String, Integer> entry : compiledGlobals) {
1031 orderedNames.add(entry.getKey());
1032 }
1033 orderedNames.addAll(carriedGlobals.keySet());
1034 orderedNames.addAll(basePersistentSeeds.keySet());
1035 orderedNames.addAll(executionUserSeeds.keySet());
1036 return new ArrayList<>(orderedNames);
1037 }
1038
1039
1040
1041
1042
1043
1044
1045 private void applyGlobalsToStack(Map<String, Object> globals) {
1046 for (Map.Entry<String, Object> entry : globals.entrySet()) {
1047 runtimeStack.setGlobalVariable(entry.getKey(), entry.getValue());
1048 }
1049 }
1050
1051
1052
1053
1054
1055
1056
1057
1058 private boolean isPersistentEligibleGlobal(String name) {
1059 return name != null && !isManagedSpecialVariable(name) && !NON_PERSISTENT_GLOBALS.contains(name);
1060 }
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073 private boolean isManagedSpecialVariable(String name) {
1074 if (!JRT.isJrtManagedSpecialVariable(name)) {
1075 return false;
1076 }
1077 if (JRT.isGawkOnlySpecialVariable(name)) {
1078 return !settings.isPosix();
1079 }
1080 return true;
1081 }
1082
1083
1084
1085
1086
1087
1088
1089
1090 private void validateSeededGlobalName(String name) {
1091 if (functionNames.contains(name)) {
1092 throw new IllegalArgumentException("Cannot assign a value to a function name (" + name + ").");
1093 }
1094 }
1095
1096
1097
1098
1099
1100
1101
1102
1103 private void validateSeededGlobalValue(String name, Object value) {
1104 Boolean arrayObj = globalVariableArrays.get(name);
1105 if (Boolean.TRUE.equals(arrayObj) && !(value instanceof Map)) {
1106 throw new IllegalArgumentException("Cannot assign a scalar to a non-scalar variable (" + name + ").");
1107 }
1108 }
1109
1110
1111
1112
1113
1114
1115
1116
1117 private void executeTuples(PositionTracker position) throws ExitException, IOException {
1118 Map<Long, ConditionPair> conditionPairs = null;
1119 Opcode opcode = null;
1120 long tupleStartNanos = 0L;
1121 try {
1122 while (!position.isEOF()) {
1123
1124 Tuple tuple = position.current();
1125 opcode = tuple.getOpcode();
1126 if (profiling) {
1127 tupleStartNanos = beforeProfiledTuple(tuple, opcode);
1128 }
1129
1130 switch (opcode) {
1131 case PRINT: {
1132 execPrint((CountTuple) tuple);
1133 position.next();
1134 break;
1135 }
1136 case PRINT_TO_FILE: {
1137 execPrintToFile((CountAndAppendTuple) tuple);
1138 position.next();
1139 break;
1140 }
1141 case PRINT_TO_PIPE: {
1142 execPrintToPipe((CountTuple) tuple);
1143 position.next();
1144 break;
1145 }
1146 case PRINTF: {
1147 execPrintf((CountTuple) tuple);
1148 position.next();
1149 break;
1150 }
1151 case PRINTF_TO_FILE: {
1152 execPrintfToFile((CountAndAppendTuple) tuple);
1153 position.next();
1154 break;
1155 }
1156 case PRINTF_TO_PIPE: {
1157 execPrintfToPipe((CountTuple) tuple);
1158 position.next();
1159 break;
1160 }
1161 case SPRINTF: {
1162
1163
1164
1165
1166 CountTuple countTuple = (CountTuple) tuple;
1167 long numArgs = countTuple.getCount();
1168 push(sprintfFunction(numArgs));
1169 position.next();
1170 break;
1171 }
1172 case LENGTH: {
1173 execLength((CountTuple) tuple);
1174 position.next();
1175 break;
1176 }
1177 case PUSH_LONG: {
1178
1179 PushLongTuple pushTuple = (PushLongTuple) tuple;
1180 push(pushTuple.getValue());
1181 position.next();
1182 break;
1183 }
1184 case PUSH_DOUBLE: {
1185
1186 PushDoubleTuple pushTuple = (PushDoubleTuple) tuple;
1187 push(pushTuple.getValue());
1188 position.next();
1189 break;
1190 }
1191 case PUSH_STRING: {
1192
1193 PushStringTuple pushTuple = (PushStringTuple) tuple;
1194 push(pushTuple.getValue());
1195 position.next();
1196 break;
1197 }
1198 case POP: {
1199
1200 Object discarded = pop();
1201 if (tuple instanceof ScalarPopTuple) {
1202 checkScalar(discarded);
1203 }
1204 position.next();
1205 break;
1206 }
1207 case IFFALSE: {
1208
1209
1210
1211
1212
1213
1214 boolean jump = !jrt.toBoolean(pop());
1215 if (jump) {
1216 position.jump(tuple.getAddress());
1217 } else {
1218 position.next();
1219 }
1220 break;
1221 }
1222 case TO_NUMBER: {
1223
1224
1225
1226
1227
1228 boolean val = jrt.toBoolean(pop());
1229 push(val ? ONE : ZERO);
1230 position.next();
1231 break;
1232 }
1233 case IFTRUE: {
1234
1235
1236
1237
1238
1239
1240 boolean jump = jrt.toBoolean(pop());
1241 if (jump) {
1242 position.jump(tuple.getAddress());
1243 } else {
1244 position.next();
1245 }
1246 break;
1247 }
1248 case NOT: {
1249
1250
1251 Object o = pop();
1252
1253 boolean result = jrt.toBoolean(o);
1254
1255 if (result) {
1256 push(0);
1257 } else {
1258 push(1);
1259 }
1260 position.next();
1261 break;
1262 }
1263 case NEGATE: {
1264
1265
1266 push(JRT.negate(pop()));
1267 position.next();
1268 break;
1269 }
1270 case UNARY_PLUS: {
1271
1272
1273
1274 push(numericValueOf(pop()));
1275 position.next();
1276 break;
1277 }
1278 case GOTO: {
1279
1280
1281 position.jump(tuple.getAddress());
1282 break;
1283 }
1284 case NOP: {
1285
1286 position.next();
1287 break;
1288 }
1289 case CONCAT: {
1290
1291
1292 String s2 = jrt.toAwkString(pop());
1293 String s1 = jrt.toAwkString(pop());
1294 String resultString = s1 + s2;
1295 push(resultString);
1296 position.next();
1297 break;
1298 }
1299 case MULTI_CONCAT: {
1300
1301
1302 CountTuple countTuple = (CountTuple) tuple;
1303 int count = (int) countTuple.getCount();
1304
1305
1306
1307
1308 String[] values = new String[count];
1309 int resultLength = 0;
1310 for (int i = count - 1; i >= 0; i--) {
1311 values[i] = jrt.toAwkString(pop());
1312 resultLength += values[i].length();
1313 }
1314 StringBuilder resultString = new StringBuilder(resultLength);
1315 for (String value : values) {
1316 resultString.append(value);
1317 }
1318 push(resultString.toString());
1319 position.next();
1320 break;
1321 }
1322 case ASSIGN:
1323 case ASSIGN_NOPUSH: {
1324
1325
1326
1327 VariableTuple variableTuple = (VariableTuple) tuple;
1328 Object value = pop();
1329 assign(variableTuple.getVariableOffset(), value, variableTuple.isGlobal(), position, opcode == Opcode.ASSIGN);
1330 position.next();
1331 break;
1332 }
1333 case ASSIGN_ARRAY: {
1334
1335
1336
1337
1338 Object arrIdx = pop();
1339 Object rhs = pop();
1340 if (rhs == null) {
1341 rhs = BLANK;
1342 }
1343 VariableTuple variableTuple = (VariableTuple) tuple;
1344 long offset = variableTuple.getVariableOffset();
1345 boolean isGlobal = variableTuple.isGlobal();
1346 assignArray(offset, arrIdx, rhs, isGlobal);
1347 position.next();
1348 break;
1349 }
1350 case ASSIGN_MAP_ELEMENT: {
1351
1352
1353
1354 Object arrIdx = pop();
1355 Map<Object, Object> array = toMap(pop());
1356 Object rhs = pop();
1357 if (rhs == null) {
1358 rhs = BLANK;
1359 }
1360 assignMapElement(array, arrIdx, rhs);
1361 position.next();
1362 break;
1363 }
1364 case PLUS_EQ_ARRAY:
1365 case MINUS_EQ_ARRAY:
1366 case MULT_EQ_ARRAY:
1367 case DIV_EQ_ARRAY:
1368 case MOD_EQ_ARRAY:
1369 case POW_EQ_ARRAY: {
1370 execCompoundAssignArray(opcode, (VariableTuple) tuple);
1371 position.next();
1372 break;
1373 }
1374 case PLUS_EQ_MAP_ELEMENT:
1375 case MINUS_EQ_MAP_ELEMENT:
1376 case MULT_EQ_MAP_ELEMENT:
1377 case DIV_EQ_MAP_ELEMENT:
1378 case MOD_EQ_MAP_ELEMENT:
1379 case POW_EQ_MAP_ELEMENT: {
1380 execCompoundAssignMapElement(opcode);
1381 position.next();
1382 break;
1383 }
1384
1385 case ASSIGN_AS_INPUT: {
1386
1387 jrt.setInputLine(pop());
1388 push(jrt.getInputLine());
1389 position.next();
1390 break;
1391 }
1392
1393 case ASSIGN_AS_INPUT_FIELD: {
1394
1395
1396 Object fieldNumObj = pop();
1397 long fieldNum = JRT.parseFieldNumber(fieldNumObj);
1398 Object value = pop();
1399 push(value);
1400 if (fieldNum == 0) {
1401 jrt.setInputLine(value);
1402 jrt.jrtParseFields();
1403 } else {
1404 jrt.jrtSetInputField(value, fieldNum);
1405 }
1406 position.next();
1407 break;
1408 }
1409 case PLUS_EQ:
1410 case MINUS_EQ:
1411 case MULT_EQ:
1412 case DIV_EQ:
1413 case MOD_EQ:
1414 case POW_EQ: {
1415 execCompoundAssignVariable(opcode, (VariableTuple) tuple);
1416 position.next();
1417 break;
1418 }
1419 case PLUS_EQ_INPUT_FIELD:
1420 case MINUS_EQ_INPUT_FIELD:
1421 case MULT_EQ_INPUT_FIELD:
1422 case DIV_EQ_INPUT_FIELD:
1423 case MOD_EQ_INPUT_FIELD:
1424 case POW_EQ_INPUT_FIELD: {
1425 execCompoundAssignInputField(opcode);
1426 position.next();
1427 break;
1428 }
1429 case INC: {
1430
1431
1432 VariableTuple variableTuple = (VariableTuple) tuple;
1433 inc(variableTuple.getVariableOffset(), variableTuple.isGlobal());
1434 position.next();
1435 break;
1436 }
1437 case DEC: {
1438
1439
1440 VariableTuple variableTuple = (VariableTuple) tuple;
1441 dec(variableTuple.getVariableOffset(), variableTuple.isGlobal());
1442 position.next();
1443 break;
1444 }
1445 case POSTINC: {
1446
1447
1448 pop();
1449 VariableTuple variableTuple = (VariableTuple) tuple;
1450 push(inc(variableTuple.getVariableOffset(), variableTuple.isGlobal()));
1451 position.next();
1452 break;
1453 }
1454 case POSTDEC: {
1455
1456
1457 pop();
1458 VariableTuple variableTuple = (VariableTuple) tuple;
1459 push(dec(variableTuple.getVariableOffset(), variableTuple.isGlobal()));
1460 position.next();
1461 break;
1462 }
1463 case INC_ARRAY_REF: {
1464
1465
1466
1467 VariableTuple variableTuple = (VariableTuple) tuple;
1468 boolean isGlobal = variableTuple.isGlobal();
1469 Map<Object, Object> aa = ensureMapVariable(variableTuple.getVariableOffset(), isGlobal);
1470 Object key = pop();
1471 checkScalar(key);
1472 Object o = aa.get(key);
1473 aa.put(key, JRT.inc(blankToZero(o)));
1474 position.next();
1475 break;
1476 }
1477 case DEC_ARRAY_REF: {
1478
1479
1480
1481 VariableTuple variableTuple = (VariableTuple) tuple;
1482 boolean isGlobal = variableTuple.isGlobal();
1483 Map<Object, Object> aa = ensureMapVariable(variableTuple.getVariableOffset(), isGlobal);
1484 Object key = pop();
1485 checkScalar(key);
1486 Object o = aa.get(key);
1487 aa.put(key, JRT.dec(blankToZero(o)));
1488 position.next();
1489 break;
1490 }
1491 case INC_MAP_REF: {
1492
1493
1494 Object key = pop();
1495 checkScalar(key);
1496 Map<Object, Object> aa = toMap(pop());
1497 Object o = aa.get(key);
1498 aa.put(key, JRT.inc(blankToZero(o)));
1499 position.next();
1500 break;
1501 }
1502 case DEC_MAP_REF: {
1503
1504
1505 Object key = pop();
1506 checkScalar(key);
1507 Map<Object, Object> aa = toMap(pop());
1508 Object o = aa.get(key);
1509 aa.put(key, JRT.dec(blankToZero(o)));
1510 position.next();
1511 break;
1512 }
1513 case INC_DOLLAR_REF: {
1514
1515 long fieldnum = JRT.parseFieldNumber(pop());
1516
1517 Object numObj = blankToZero(jrt.jrtGetInputField(fieldnum));
1518 Object original = numericValueOf(numObj);
1519 setNumOnJRT(fieldnum, JRT.inc(original));
1520
1521 push(original);
1522
1523 position.next();
1524 break;
1525 }
1526 case DEC_DOLLAR_REF: {
1527
1528
1529 long fieldnum = JRT.parseFieldNumber(pop());
1530
1531 Object numObj = blankToZero(jrt.jrtGetInputField(fieldnum));
1532 Object original = numericValueOf(numObj);
1533 setNumOnJRT(fieldnum, JRT.dec(original));
1534
1535 push(original);
1536
1537 position.next();
1538 break;
1539 }
1540 case DEREFERENCE: {
1541
1542
1543 DereferenceTuple dereferenceTuple = (DereferenceTuple) tuple;
1544 boolean isGlobal = dereferenceTuple.isGlobal();
1545 long offset = dereferenceTuple.getVariableOffset();
1546 push(resolveVariable(offset, isGlobal, dereferenceTuple.isArray()));
1547 position.next();
1548 break;
1549 }
1550 case PEEK_DEREFERENCE: {
1551 VariableTuple variableTuple = (VariableTuple) tuple;
1552 Object value = runtimeStack.getVariable(variableTuple.getVariableOffset(), variableTuple.isGlobal());
1553 push(value instanceof ArgumentReference ? resolveRawArgumentReference((ArgumentReference) value) : value);
1554 position.next();
1555 break;
1556 }
1557 case PUSH_INDIRECT_ARGUMENT: {
1558 VariableTuple variableTuple = (VariableTuple) tuple;
1559 Object scalarValue = runtimeStack.getVariable(variableTuple.getVariableOffset(), variableTuple.isGlobal());
1560
1561
1562 if (scalarValue instanceof ArgumentReference) {
1563 push(resolveUserFunctionArgument(scalarValue));
1564 } else if (isUntyped(scalarValue)) {
1565 push(
1566 new IndirectArgumentReference(
1567 runtimeStack.getVariableFrame(variableTuple.isGlobal()),
1568 variableTuple.getVariableOffset(),
1569 scalarValue));
1570 } else {
1571 push(scalarValue);
1572 }
1573 position.next();
1574 break;
1575 }
1576 case PUSH_INDIRECT_ARRAY_ARGUMENT: {
1577 Object idx = pop();
1578 checkScalar(idx);
1579 Map<Object, Object> map = toMap(pop());
1580 Object scalarValue = JRT.getAssocArrayValue(map, idx);
1581
1582
1583 push(isUntyped(scalarValue) ? new IndirectArrayArgumentReference(map, idx, scalarValue) : scalarValue);
1584 position.next();
1585 break;
1586 }
1587 case DEREF_ARRAY: {
1588
1589 Object idx = pop();
1590 checkScalar(idx);
1591 Map<Object, Object> map = toMap(pop());
1592 Object o = JRT.getAssocArrayValue(map, idx);
1593 push(o);
1594 position.next();
1595 break;
1596 }
1597 case ENSURE_ARRAY_ELEMENT: {
1598
1599
1600 Object idx = pop();
1601 Map<Object, Object> map = toMap(pop());
1602 push(ensureArrayInArray(map, idx));
1603 position.next();
1604 break;
1605 }
1606 case PEEK_ARRAY_ELEMENT: {
1607
1608 Object idx = pop();
1609 checkScalar(idx);
1610 Map<Object, Object> map = toMap(pop());
1611 if (map instanceof AssocArray && !JRT.containsAwkKey(map, idx)) {
1612 push(AssocArray.UNTYPED);
1613 } else {
1614 Object value = map.get(idx);
1615 push(value != null ? value : AssocArray.UNTYPED);
1616 }
1617 position.next();
1618 break;
1619 }
1620 case SRAND: {
1621
1622
1623 CountTuple countTuple = (CountTuple) tuple;
1624 long numArgs = countTuple.getCount();
1625 int seed;
1626 if (numArgs == 0) {
1627
1628 seed = JRT.timeSeed();
1629 } else {
1630 seed = (int) JRT.toDouble(pop());
1631 }
1632 int previousSeed = randomNumberGenerator.getSeed();
1633 randomNumberGenerator.setSeed(seed);
1634 push(previousSeed);
1635 position.next();
1636 break;
1637 }
1638 case RAND: {
1639 push(randomNumberGenerator.nextDouble());
1640 position.next();
1641 break;
1642 }
1643 case INTFUNC: {
1644
1645 push(JRT.truncateToScalar(JRT.toDouble(pop())));
1646 position.next();
1647 break;
1648 }
1649 case SQRT: {
1650
1651 push(Math.sqrt(JRT.toDouble(pop())));
1652 position.next();
1653 break;
1654 }
1655 case LOG: {
1656
1657 push(Math.log(JRT.toDouble(pop())));
1658 position.next();
1659 break;
1660 }
1661 case EXP: {
1662
1663 push(Math.exp(JRT.toDouble(pop())));
1664 position.next();
1665 break;
1666 }
1667 case SIN: {
1668
1669 push(Math.sin(JRT.toDouble(pop())));
1670 position.next();
1671 break;
1672 }
1673 case COS: {
1674
1675 push(Math.cos(JRT.toDouble(pop())));
1676 position.next();
1677 break;
1678 }
1679 case ATAN2: {
1680
1681
1682 double d2 = JRT.toDouble(pop());
1683 double d1 = JRT.toDouble(pop());
1684 push(Math.atan2(d1, d2));
1685 position.next();
1686 break;
1687 }
1688 case MATCH: {
1689 execMatch();
1690 position.next();
1691 break;
1692 }
1693 case INDEX: {
1694
1695
1696 String s2 = jrt.toAwkString(pop());
1697 String s1 = jrt.toAwkString(pop());
1698 push(jrt.index(s1, s2));
1699 position.next();
1700 break;
1701 }
1702 case SUB_FOR_DOLLAR_0: {
1703 execSubForDollar0((BooleanTuple) tuple);
1704 position.next();
1705 break;
1706 }
1707 case SUB_FOR_DOLLAR_REFERENCE: {
1708 execSubForDollarReference((BooleanTuple) tuple);
1709 position.next();
1710 break;
1711 }
1712 case SUB_FOR_VARIABLE: {
1713 execSubForVariable((SubstitutionVariableTuple) tuple, position);
1714 position.next();
1715 break;
1716 }
1717 case SUB_FOR_ARRAY_REFERENCE: {
1718 execSubForArrayReference((SubstitutionVariableTuple) tuple);
1719 position.next();
1720 break;
1721 }
1722 case SUB_FOR_MAP_REFERENCE: {
1723 execSubForMapReference((BooleanTuple) tuple);
1724 position.next();
1725 break;
1726 }
1727 case SPLIT: {
1728 execSplit((CountTuple) tuple, position);
1729 position.next();
1730 break;
1731 }
1732 case SUBSTR: {
1733 execSubstr((CountTuple) tuple);
1734 position.next();
1735 break;
1736 }
1737 case TOLOWER: {
1738
1739 push(jrt.toAwkString(pop()).toLowerCase());
1740 position.next();
1741 break;
1742 }
1743 case TOUPPER: {
1744
1745 push(jrt.toAwkString(pop()).toUpperCase());
1746 position.next();
1747 break;
1748 }
1749 case SYSTEM: {
1750
1751 String s = jrt.toAwkString(pop());
1752 push(jrt.jrtSystem(s));
1753 position.next();
1754 break;
1755 }
1756 case SWAP: {
1757
1758
1759 Object o1 = pop();
1760 Object o2 = pop();
1761 push(o1);
1762 push(o2);
1763 position.next();
1764 break;
1765 }
1766 case CMP_EQ: {
1767
1768
1769 Object o2 = pop();
1770 Object o1 = pop();
1771 push(jrt.compare(o1, o2, 0) ? ONE : ZERO);
1772 position.next();
1773 break;
1774 }
1775 case CMP_LT: {
1776
1777
1778 Object o2 = pop();
1779 Object o1 = pop();
1780 push(jrt.compare(o1, o2, -1) ? ONE : ZERO);
1781 position.next();
1782 break;
1783 }
1784 case CMP_GT: {
1785
1786
1787 Object o2 = pop();
1788 Object o1 = pop();
1789 push(jrt.compare(o1, o2, 1) ? ONE : ZERO);
1790 position.next();
1791 break;
1792 }
1793 case MATCHES: {
1794
1795
1796 Object o2 = pop();
1797 Object o1 = pop();
1798
1799
1800 push(jrt.matches(jrt.toAwkString(o1), o2) ? 1 : 0);
1801 position.next();
1802 break;
1803 }
1804 case ADD: {
1805
1806
1807 Object o2 = pop();
1808 Object o1 = pop();
1809 push(JRT.add(o1, o2));
1810 position.next();
1811 break;
1812 }
1813 case SUBTRACT: {
1814
1815
1816 Object o2 = pop();
1817 Object o1 = pop();
1818 push(JRT.subtract(o1, o2));
1819 position.next();
1820 break;
1821 }
1822 case MULTIPLY: {
1823
1824
1825 Object o2 = pop();
1826 Object o1 = pop();
1827 push(JRT.multiply(o1, o2));
1828 position.next();
1829 break;
1830 }
1831 case DIVIDE: {
1832
1833
1834 Object o2 = pop();
1835 Object o1 = pop();
1836 push(JRT.divide(o1, o2));
1837 position.next();
1838 break;
1839 }
1840 case MOD: {
1841
1842
1843 Object o2 = pop();
1844 Object o1 = pop();
1845 push(JRT.mod(o1, o2));
1846 position.next();
1847 break;
1848 }
1849 case POW: {
1850
1851
1852 Object o2 = pop();
1853 Object o1 = pop();
1854 push(JRT.pow(o1, o2));
1855 position.next();
1856 break;
1857 }
1858 case DUP: {
1859
1860 Object o = pop();
1861 push(o);
1862 push(o);
1863 position.next();
1864 break;
1865 }
1866 case KEYLIST: {
1867 Object o = pop();
1868 if (isUntyped(o)) {
1869 push(new ArrayDeque<>());
1870 position.next();
1871 break;
1872 }
1873 if (!(o instanceof Map)) {
1874 throw new AwkRuntimeException("Attempting to use a scalar as an array.");
1875 }
1876 @SuppressWarnings("unchecked")
1877 Map<Object, Object> map = (Map<Object, Object>) o;
1878 push(new ArrayDeque<>(forInKeyOrder == null ? map.keySet() : forInKeyOrder.order(map)));
1879 position.next();
1880 break;
1881 }
1882 case IS_EMPTY_KEYLIST: {
1883
1884
1885 Object o = pop();
1886 if (o == null || !(o instanceof Deque)) {
1887 throw new AwkRuntimeException(
1888 position.lineNumber(),
1889 "Cannot get a key list (via 'in') of a non associative array. arg = " + o.getClass() + ", " + o);
1890 }
1891 Deque<?> keylist = (Deque<?>) o;
1892 if (keylist.isEmpty()) {
1893 position.jump(tuple.getAddress());
1894 } else {
1895 position.next();
1896 }
1897 break;
1898 }
1899 case GET_FIRST_AND_REMOVE_FROM_KEYLIST: {
1900
1901 Object o = pop();
1902 if (o == null || !(o instanceof Deque)) {
1903 throw new AwkRuntimeException(
1904 position.lineNumber(),
1905 "Cannot get a key list (via 'in') of a non associative array. arg = " + o.getClass() + ", " + o);
1906 }
1907
1908 Deque<?> keylist = (Deque<?>) o;
1909 push(keylist.removeFirst());
1910 position.next();
1911 break;
1912 }
1913 case CHECK_CLASS: {
1914
1915
1916 ClassTuple checkTuple = (ClassTuple) tuple;
1917 Object o = pop();
1918 if (!checkTuple.getType().isInstance(o)) {
1919 throw new AwkRuntimeException(
1920 position.lineNumber(),
1921 "Verification failed. Top-of-stack = " + o.getClass() + " isn't an instance of "
1922 + checkTuple.getType());
1923 }
1924 push(o);
1925 position.next();
1926 break;
1927 }
1928 case CONSUME_INPUT: {
1929
1930
1931 mainInputLoopStarted = true;
1932 if (jrt.consumeInput(resolvedInputSource)) {
1933 position.next();
1934 } else {
1935 position.jump(tuple.getAddress());
1936 }
1937 break;
1938 }
1939 case CONSUME_FILE_INPUT: {
1940
1941
1942 mainInputLoopStarted = true;
1943 withinBeginFileBlocks = false;
1944 if (jrt.consumeCurrentFileInput(resolvedInputSource)) {
1945 position.next();
1946 } else {
1947 withinEndFileBlocks = true;
1948 position.jump(tuple.getAddress());
1949 }
1950 break;
1951 }
1952 case NEXT_FILE: {
1953
1954 inputFileLoopStarted = true;
1955 withinEndFileBlocks = false;
1956 if (jrt.advanceToNextFile(resolvedInputSource)) {
1957 withinBeginFileBlocks = true;
1958 position.next();
1959 } else {
1960 position.jump(tuple.getAddress());
1961 }
1962 break;
1963 }
1964 case EXEC_NEXTFILE: {
1965 executeNextfile(position);
1966 break;
1967 }
1968 case EXEC_NEXT: {
1969 executeNext(position);
1970 break;
1971 }
1972
1973 case GETLINE_INPUT: {
1974 checkGetlineAllowed(position);
1975 boolean consumed = isMainInputFileBounded() ?
1976 jrt.consumeCurrentFileInput(resolvedInputSource) :
1977 jrt.consumeInput(resolvedInputSource);
1978 push(consumed ? 1 : 0);
1979 position.next();
1980 break;
1981 }
1982 case GETLINE_INPUT_TO_TARGET: {
1983
1984 checkGetlineAllowed(position);
1985 Object input = isMainInputFileBounded() ?
1986 jrt.consumeCurrentFileInputToTarget(resolvedInputSource) :
1987 jrt.consumeInputToTarget(resolvedInputSource);
1988 if (input != null) {
1989 push(1);
1990 push(input);
1991 position.next();
1992 } else {
1993 push(0);
1994 position.jump(tuple.getAddress());
1995 }
1996 break;
1997 }
1998 case USE_AS_FILE_INPUT: {
1999
2000
2001 String s = jrt.toAwkString(pop());
2002 Integer retcode = jrt.jrtConsumeFileInputForGetline(s);
2003 push(retcode);
2004 if (retcode.intValue() == 1) {
2005 push(jrt.toInputScalar(jrt.jrtGetInputString()));
2006 position.next();
2007 } else {
2008 position.jump(tuple.getAddress());
2009 }
2010 break;
2011 }
2012 case USE_AS_COMMAND_INPUT: {
2013
2014
2015 String s = jrt.toAwkString(pop());
2016 Integer retcode = jrt.jrtConsumeCommandInputForGetline(s);
2017 push(retcode);
2018 if (retcode.intValue() == 1) {
2019 push(jrt.toInputScalar(jrt.jrtGetInputString()));
2020 position.next();
2021 } else {
2022 position.jump(tuple.getAddress());
2023 }
2024 break;
2025 }
2026 case ENVIRON_OFFSET: {
2027
2028
2029 populateEnviron(((LongTuple) tuple).getValue());
2030 position.next();
2031 break;
2032 }
2033 case ARGC_OFFSET: {
2034
2035 populateArgc(((LongTuple) tuple).getValue());
2036 position.next();
2037 break;
2038 }
2039 case ARGV_OFFSET: {
2040
2041 populateArgv(((LongTuple) tuple).getValue());
2042 position.next();
2043 break;
2044 }
2045 case GET_INPUT_FIELD: {
2046
2047 Object fieldNumber = pop();
2048 push(jrt.jrtGetInputField(fieldNumber));
2049 position.next();
2050 break;
2051 }
2052 case GET_INPUT_FIELD_CONST: {
2053 InputFieldTuple inputFieldTuple = (InputFieldTuple) tuple;
2054 long fieldnum = inputFieldTuple.getFieldIndex();
2055 push(jrt.jrtGetInputField(fieldnum));
2056 position.next();
2057 break;
2058 }
2059 case APPLY_RS: {
2060 jrt.applyRS(jrt.getRSVar());
2061 position.next();
2062 break;
2063 }
2064 case CALL_FUNCTION: {
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074 CallFunctionTuple callTuple = (CallFunctionTuple) tuple;
2075 Address funcAddr = callTuple.getAddress();
2076 long numFormalParams = callTuple.getNumFormalParams();
2077 long numActualParams = callTuple.getNumActualParams();
2078 runtimeStack.pushFrame(numFormalParams, position.currentIndex());
2079
2080 for (long i = numActualParams - 1; i >= 0; i--) {
2081 Object argument = pop();
2082 adoptElementArgumentReference(argument);
2083 runtimeStack.setVariable(i, argument, false);
2084 }
2085 position.jump(funcAddr);
2086
2087 break;
2088 }
2089 case INDIRECT_CALL: {
2090 execIndirectCall((IndirectCallTuple) tuple, position, tupleStartNanos);
2091 break;
2092 }
2093 case FUNCTION: {
2094
2095
2096
2097
2098 position.next();
2099 break;
2100 }
2101 case WARNING: {
2102 jrt.printWarning(((Tuple.WarningTuple) tuple).getMessage());
2103 position.next();
2104 break;
2105 }
2106 case SET_RETURN_RESULT: {
2107
2108 runtimeStack.setReturnValue(pop());
2109 position.next();
2110 break;
2111 }
2112 case RETURN_FROM_FUNCTION: {
2113 releaseElementArgumentReferences();
2114 position.jump(runtimeStack.popFrame());
2115 push(runtimeStack.getReturnValue());
2116 position.next();
2117 break;
2118 }
2119 case SET_NUM_GLOBALS: {
2120 execSetNumGlobals((CountTuple) tuple);
2121 position.next();
2122 break;
2123 }
2124 case BEFORE_START_HOOKS: {
2125 runBeforeStartHooks();
2126 position.next();
2127 break;
2128 }
2129 case UPDATE_SYMTAB: {
2130 execUpdateSymtab(((LongTuple) tuple).getValue());
2131 position.next();
2132 break;
2133 }
2134 case UPDATE_FUNCTAB: {
2135 execUpdateFunctab(((LongTuple) tuple).getValue());
2136 position.next();
2137 break;
2138 }
2139 case CLOSE: {
2140
2141 String s = jrt.toAwkString(pop());
2142 push(jrt.jrtClose(s));
2143 position.next();
2144 break;
2145 }
2146 case APPLY_SUBSEP: {
2147 execApplySubsep((CountTuple) tuple);
2148 position.next();
2149 break;
2150 }
2151 case APPLY_SUBSEP_UNDER_TOP: {
2152
2153
2154 Object top = pop();
2155 execApplySubsep((CountTuple) tuple);
2156 push(top);
2157 position.next();
2158 break;
2159 }
2160 case DELETE_ARRAY_ELEMENT: {
2161
2162
2163
2164 VariableTuple variableTuple = (VariableTuple) tuple;
2165 long offset = variableTuple.getVariableOffset();
2166 boolean isGlobal = variableTuple.isGlobal();
2167 Map<Object, Object> aa = getMapVariable(offset, isGlobal);
2168 Object key = pop();
2169 checkScalar(key);
2170 if (aa != null) {
2171 aa.remove(key);
2172 detachMissingArrayArgumentReferences(aa);
2173 }
2174 position.next();
2175 break;
2176 }
2177 case DELETE_MAP_ELEMENT: {
2178
2179
2180 Object key = pop();
2181 checkScalar(key);
2182 Map<Object, Object> aa = toMap(pop());
2183 aa.remove(key);
2184 detachMissingArrayArgumentReferences(aa);
2185 position.next();
2186 break;
2187 }
2188 case DELETE_ARRAY: {
2189
2190
2191
2192 VariableTuple variableTuple = (VariableTuple) tuple;
2193 long offset = variableTuple.getVariableOffset();
2194 boolean isGlobal = variableTuple.isGlobal();
2195 Map<Object, Object> array = getMapVariable(offset, isGlobal);
2196 if (array != null) {
2197 array.clear();
2198 detachMissingArrayArgumentReferences(array);
2199 }
2200 position.next();
2201 break;
2202 }
2203 case SET_WITHIN_END_BLOCKS: {
2204
2205 BooleanTuple endBlocksTuple = (BooleanTuple) tuple;
2206 withinEndBlocks = endBlocksTuple.getValue();
2207 position.next();
2208 break;
2209 }
2210 case EXIT_WITHOUT_CODE:
2211 case EXIT_WITH_CODE: {
2212 if (opcode == Opcode.EXIT_WITH_CODE) {
2213
2214 exitCode = (int) JRT.toDouble(pop());
2215 }
2216 throwExitException = true;
2217 withinBeginFileBlocks = false;
2218 withinEndFileBlocks = false;
2219
2220
2221 if (!withinEndBlocks && exitAddress != null) {
2222 resetCallState();
2223 position.jump(exitAddress);
2224 } else {
2225
2226
2227 clearOperandStack();
2228 throw new ExitException(exitCode, "The AWK script requested an exit");
2229
2230 }
2231 break;
2232 }
2233 case REGEXP: {
2234
2235 RegexTuple regexTuple = (RegexTuple) tuple;
2236 Pattern pattern = regexTuple.getPattern();
2237 push(pattern);
2238 position.next();
2239 break;
2240 }
2241 case CONDITION_PAIR: {
2242
2243
2244
2245 if (conditionPairs == null) {
2246 conditionPairs = new HashMap<Long, ConditionPair>();
2247 }
2248 long currentIndex = position.currentIndex();
2249 ConditionPair cp = conditionPairs.get(currentIndex);
2250 if (cp == null) {
2251 cp = new ConditionPair();
2252 conditionPairs.put(currentIndex, cp);
2253 }
2254 boolean end = jrt.toBoolean(pop());
2255 boolean start = jrt.toBoolean(pop());
2256 push(cp.update(start, end) ? ONE : ZERO);
2257 position.next();
2258 break;
2259 }
2260 case CONDITION_PAIR_IN_RANGE: {
2261
2262 if (conditionPairs == null) {
2263 conditionPairs = new HashMap<Long, ConditionPair>();
2264 }
2265 long id = ((LongTuple) tuple).getValue();
2266 ConditionPair cp = conditionPairs.get(id);
2267 if (cp == null) {
2268 cp = new ConditionPair();
2269 conditionPairs.put(id, cp);
2270 }
2271 push(cp.isWithin() ? ONE : ZERO);
2272 position.next();
2273 break;
2274 }
2275 case CONDITION_PAIR_ENTER: {
2276
2277
2278
2279 conditionPairs.get(((LongTuple) tuple).getValue()).enter();
2280 position.next();
2281 break;
2282 }
2283 case CONDITION_PAIR_LEAVE: {
2284
2285 conditionPairs.get(((LongTuple) tuple).getValue()).leave();
2286 position.next();
2287 break;
2288 }
2289 case IS_IN: {
2290
2291 Object arr = pop();
2292 Object arg = pop();
2293 checkScalar(arg);
2294 if (isUntyped(arr)) {
2295 push(ZERO);
2296 position.next();
2297 break;
2298 }
2299 if (!(arr instanceof Map)) {
2300 throw new AwkRuntimeException("Attempting to use a scalar as an array.");
2301 }
2302 @SuppressWarnings("unchecked")
2303 Map<Object, Object> aa = (Map<Object, Object>) arr;
2304 boolean result = JRT.containsAwkKey(aa, arg);
2305 push(result ? ONE : ZERO);
2306 position.next();
2307 break;
2308 }
2309 case THIS: {
2310
2311
2312
2313
2314 position.next();
2315 break;
2316 }
2317 case EXTENSION: {
2318
2319
2320
2321
2322
2323 ExtensionTuple extensionTuple = (ExtensionTuple) tuple;
2324 ExtensionFunction function = extensionTuple.getFunction();
2325 long numArgs = extensionTuple.getArgCount();
2326 push(invokeExtension(function, popArguments(numArgs), position.lineNumber()));
2327
2328 position.next();
2329 break;
2330 }
2331 case ASSIGN_NF: {
2332 Object v = pop();
2333 jrt.setNF(v);
2334 push(v);
2335 position.next();
2336 break;
2337 }
2338 case PUSH_NF: {
2339 push(jrt.getNF());
2340 position.next();
2341 break;
2342 }
2343 case ASSIGN_NR: {
2344 Object v = pop();
2345 jrt.setNR(v);
2346 push(v);
2347 position.next();
2348 break;
2349 }
2350 case PUSH_NR: {
2351 push(jrt.getNR());
2352 position.next();
2353 break;
2354 }
2355 case ASSIGN_FNR: {
2356 Object v = pop();
2357 jrt.setFNR(v);
2358 push(v);
2359 position.next();
2360 break;
2361 }
2362 case PUSH_FNR: {
2363 push(jrt.getFNR());
2364 position.next();
2365 break;
2366 }
2367 case ASSIGN_FS: {
2368 Object v = pop();
2369 jrt.setFS(v);
2370 push(v);
2371 position.next();
2372 break;
2373 }
2374 case ASSIGN_IGNORECASE: {
2375 Object v = pop();
2376 jrt.setIGNORECASE(v);
2377 push(v);
2378 position.next();
2379 break;
2380 }
2381 case PUSH_IGNORECASE: {
2382 push(jrt.getIGNORECASEVar());
2383 position.next();
2384 break;
2385 }
2386 case PUSH_FS: {
2387 push(jrt.getFSVar());
2388 position.next();
2389 break;
2390 }
2391 case ASSIGN_RS: {
2392 Object v = pop();
2393 jrt.setRS(v);
2394 push(v);
2395 position.next();
2396 break;
2397 }
2398 case PUSH_RS: {
2399 push(jrt.getRSVar());
2400 position.next();
2401 break;
2402 }
2403 case ASSIGN_OFS: {
2404 Object v = pop();
2405 jrt.setOFS(v);
2406 push(v);
2407 position.next();
2408 break;
2409 }
2410 case PUSH_OFS: {
2411 push(jrt.getOFSVar());
2412 position.next();
2413 break;
2414 }
2415 case ASSIGN_ORS: {
2416 Object v = pop();
2417 jrt.setORS(v);
2418 push(v);
2419 position.next();
2420 break;
2421 }
2422 case PUSH_ORS: {
2423 push(jrt.getORSVar());
2424 position.next();
2425 break;
2426 }
2427 case ASSIGN_RSTART: {
2428 Object v = pop();
2429 jrt.setRSTART(v);
2430 push(v);
2431 position.next();
2432 break;
2433 }
2434 case PUSH_RSTART: {
2435 push(jrt.getRSTART());
2436 position.next();
2437 break;
2438 }
2439 case ASSIGN_RLENGTH: {
2440 Object v = pop();
2441 jrt.setRLENGTH(v);
2442 push(v);
2443 position.next();
2444 break;
2445 }
2446 case PUSH_RLENGTH: {
2447 push(jrt.getRLENGTH());
2448 position.next();
2449 break;
2450 }
2451 case ASSIGN_FILENAME: {
2452 Object v = pop();
2453 jrt.setFILENAMEViaJrt(v);
2454 push(v == null ? "" : v);
2455 position.next();
2456 break;
2457 }
2458 case PUSH_FILENAME: {
2459 push(jrt.getFILENAME());
2460 position.next();
2461 break;
2462 }
2463 case ASSIGN_ERRNO: {
2464 Object v = pop();
2465 jrt.setERRNO(v);
2466 push(v == null ? "" : v);
2467 position.next();
2468 break;
2469 }
2470 case PUSH_ERRNO: {
2471 push(jrt.getERRNO());
2472 position.next();
2473 break;
2474 }
2475 case ASSIGN_ARGIND: {
2476 Object v = pop();
2477 jrt.setARGIND(v);
2478 push(v == null ? ZERO : v);
2479 position.next();
2480 break;
2481 }
2482 case PUSH_ARGIND: {
2483 push(jrt.getARGIND());
2484 position.next();
2485 break;
2486 }
2487 case ASSIGN_SUBSEP: {
2488 Object v = pop();
2489 jrt.setSUBSEP(v);
2490 push(v);
2491 position.next();
2492 break;
2493 }
2494 case PUSH_SUBSEP: {
2495 push(jrt.getSUBSEPVar());
2496 position.next();
2497 break;
2498 }
2499 case ASSIGN_CONVFMT: {
2500 Object v = pop();
2501 jrt.setCONVFMT(v);
2502 push(v);
2503 position.next();
2504 break;
2505 }
2506 case PUSH_CONVFMT: {
2507 push(jrt.getCONVFMTVar());
2508 position.next();
2509 break;
2510 }
2511 case ASSIGN_OFMT: {
2512 Object v = pop();
2513 jrt.setOFMT(v);
2514 push(v);
2515 position.next();
2516 break;
2517 }
2518 case PUSH_OFMT: {
2519 push(getOFMT());
2520 position.next();
2521 break;
2522 }
2523 case ASSIGN_ARGC: {
2524 Object v = pop();
2525 if (argcOffset == NULL_OFFSET) {
2526 throw new AwkRuntimeException("ARGC is read-only (not materialized).");
2527 }
2528 runtimeStack.setVariable(argcOffset, v, true);
2529 push(v);
2530 position.next();
2531 break;
2532 }
2533 case PUSH_ARGC: {
2534 if (argcOffset == NULL_OFFSET) {
2535 push(getARGC());
2536 } else {
2537 push(runtimeStack.getVariable(argcOffset, true));
2538 }
2539 position.next();
2540 break;
2541 }
2542 default:
2543 throw new Error("invalid opcode: " + position.opcode());
2544 }
2545 if (profiling) {
2546 afterProfiledTuple(opcode, tupleStartNanos);
2547 }
2548 }
2549
2550 } catch (ExitException ee) {
2551 if (profiling && (opcode == Opcode.EXIT_WITH_CODE || opcode == Opcode.EXIT_WITHOUT_CODE)) {
2552 afterProfiledTuple(opcode, tupleStartNanos);
2553 }
2554 throw ee;
2555 } catch (IOException ioe) {
2556 resetCallState();
2557 throw ioe;
2558 } catch (RuntimeException re) {
2559 resetCallState();
2560 if (re instanceof AwkSandboxException) {
2561 throw re;
2562 }
2563 throw new AwkRuntimeException(position.lineNumber(), re.getMessage(), re);
2564 } catch (AssertionError ae) {
2565 resetCallState();
2566 throw ae;
2567 }
2568
2569
2570 if (throwExitException) {
2571 throw new ExitException(exitCode, "The AWK script requested an exit");
2572 }
2573 }
2574
2575
2576
2577
2578 public void resetProfiling() {
2579 if (!profiling) {
2580 return;
2581 }
2582 tupleProfilingStats.clear();
2583 functionProfilingStats.clear();
2584 activeProfilingFunctions.clear();
2585 }
2586
2587
2588
2589
2590
2591
2592 public ProfilingReport getProfilingReport() {
2593 if (!profiling) {
2594 return ProfilingReport.empty();
2595 }
2596 return new ProfilingReport(tupleProfilingStats, functionProfilingStats);
2597 }
2598
2599
2600
2601
2602
2603 private void execCompoundAssignVariable(Opcode opcode, VariableTuple variableTuple) {
2604
2605
2606
2607 long offset = variableTuple.getVariableOffset();
2608 boolean isGlobal = variableTuple.isGlobal();
2609 Object o1 = blankToZero(resolveVariable(offset, isGlobal, false));
2610 Object o2 = pop();
2611 Object ans;
2612 switch (opcode) {
2613 case PLUS_EQ:
2614 ans = JRT.add(o1, o2);
2615 break;
2616 case MINUS_EQ:
2617 ans = JRT.subtract(o1, o2);
2618 break;
2619 case MULT_EQ:
2620 ans = JRT.multiply(o1, o2);
2621 break;
2622 case DIV_EQ:
2623 ans = JRT.divide(o1, o2);
2624 break;
2625 case MOD_EQ:
2626 ans = JRT.mod(o1, o2);
2627 break;
2628 case POW_EQ:
2629 ans = JRT.pow(o1, o2);
2630 break;
2631 default:
2632 throw new Error("Invalid opcode here: " + opcode);
2633 }
2634 push(ans);
2635 runtimeStack.setVariable(offset, ans, isGlobal);
2636 }
2637
2638 private void execCompoundAssignArray(Opcode opcode, VariableTuple variableTuple) {
2639
2640
2641
2642
2643 Object arrIdx = pop();
2644 Object rhs = pop();
2645 if (rhs == null) {
2646 rhs = BLANK;
2647 }
2648 long offset = variableTuple.getVariableOffset();
2649 boolean isGlobal = variableTuple.isGlobal();
2650
2651 Map<Object, Object> array = ensureMapVariable(offset, isGlobal);
2652 checkScalar(arrIdx);
2653 Object o = blankToZero(array.get(arrIdx));
2654
2655 Object newVal;
2656
2657 switch (opcode) {
2658 case PLUS_EQ_ARRAY:
2659 newVal = JRT.add(o, rhs);
2660 break;
2661 case MINUS_EQ_ARRAY:
2662 newVal = JRT.subtract(o, rhs);
2663 break;
2664 case MULT_EQ_ARRAY:
2665 newVal = JRT.multiply(o, rhs);
2666 break;
2667 case DIV_EQ_ARRAY:
2668 newVal = JRT.divide(o, rhs);
2669 break;
2670 case MOD_EQ_ARRAY:
2671 newVal = JRT.mod(o, rhs);
2672 break;
2673 case POW_EQ_ARRAY:
2674 newVal = JRT.pow(o, rhs);
2675 break;
2676 default:
2677 throw new Error("Invalid op code here: " + opcode);
2678 }
2679
2680 assignArray(offset, arrIdx, newVal, isGlobal);
2681 }
2682
2683 private void execCompoundAssignMapElement(Opcode opcode) {
2684
2685
2686
2687 Object arrIdx = pop();
2688 Map<Object, Object> array = toMap(pop());
2689 Object rhs = pop();
2690 if (rhs == null) {
2691 rhs = BLANK;
2692 }
2693
2694 checkScalar(arrIdx);
2695 Object o = blankToZero(array.get(arrIdx));
2696 Object newVal;
2697
2698 switch (opcode) {
2699 case PLUS_EQ_MAP_ELEMENT:
2700 newVal = JRT.add(o, rhs);
2701 break;
2702 case MINUS_EQ_MAP_ELEMENT:
2703 newVal = JRT.subtract(o, rhs);
2704 break;
2705 case MULT_EQ_MAP_ELEMENT:
2706 newVal = JRT.multiply(o, rhs);
2707 break;
2708 case DIV_EQ_MAP_ELEMENT:
2709 newVal = JRT.divide(o, rhs);
2710 break;
2711 case MOD_EQ_MAP_ELEMENT:
2712 newVal = JRT.mod(o, rhs);
2713 break;
2714 case POW_EQ_MAP_ELEMENT:
2715 newVal = JRT.pow(o, rhs);
2716 break;
2717 default:
2718 throw new Error("Invalid op code here: " + opcode);
2719 }
2720
2721 assignMapElement(array, arrIdx, newVal);
2722 }
2723
2724 private void execCompoundAssignInputField(Opcode opcode) {
2725
2726
2727
2728
2729 long fieldnum = JRT.parseFieldNumber(pop());
2730 Object incval = pop();
2731
2732
2733 Object numObj = blankToZero(jrt.jrtGetInputField(fieldnum));
2734 Object num;
2735 switch (opcode) {
2736 case PLUS_EQ_INPUT_FIELD:
2737 num = JRT.add(numObj, incval);
2738 break;
2739 case MINUS_EQ_INPUT_FIELD:
2740 num = JRT.subtract(numObj, incval);
2741 break;
2742 case MULT_EQ_INPUT_FIELD:
2743 num = JRT.multiply(numObj, incval);
2744 break;
2745 case DIV_EQ_INPUT_FIELD:
2746 num = JRT.divide(numObj, incval);
2747 break;
2748 case MOD_EQ_INPUT_FIELD:
2749 num = JRT.mod(numObj, incval);
2750 break;
2751 case POW_EQ_INPUT_FIELD:
2752 num = JRT.pow(numObj, incval);
2753 break;
2754 default:
2755 throw new Error("Invalid opcode here: " + opcode);
2756 }
2757 setNumOnJRT(fieldnum, num);
2758
2759
2760 push(num);
2761 }
2762
2763 private void execIndirectCall(IndirectCallTuple callTuple, PositionTracker position, long tupleStartNanos) {
2764 Object[] actualArguments = popArguments(callTuple.getNumActualParams());
2765 String requestedName = jrt.toAwkString(pop());
2766 String qualifiedName = normalizeIndirectFunctionName(requestedName);
2767 IndirectFunctionTarget target = callTuple.getUserFunctions().get(qualifiedName);
2768 if (target != null) {
2769 long formalCount = target.getNumFormalParams();
2770 if (actualArguments.length > formalCount) {
2771 jrt.printWarning(
2772 "gawk: " + callTuple.getSourceName() + ":" + callTuple.getSourceLine() + ": warning: function `"
2773 + qualifiedName + "' called with more arguments than declared");
2774 }
2775 if (profiling) {
2776 activeProfilingFunctions.push(new ActiveFunction(qualifiedName, tupleStartNanos));
2777 }
2778 runtimeStack.pushFrame(formalCount, position.currentIndex());
2779 adoptElementArgumentReferences(actualArguments);
2780 int copiedArgumentCount = Math.min(actualArguments.length, (int) formalCount);
2781 for (int i = 0; i < copiedArgumentCount; i++) {
2782 runtimeStack.setVariable(i, actualArguments[i], false);
2783 }
2784 position.jump(target.getAddress());
2785 return;
2786 }
2787
2788 String awkName = requestedName.startsWith("awk::") ? requestedName.substring("awk::".length()) : requestedName;
2789 BuiltinFunction builtin = BuiltinFunction.of(awkName);
2790 if (builtin != null) {
2791 resolveIndirectArguments(actualArguments, builtin);
2792 push(invokeIndirectBuiltin(builtin, actualArguments, position.lineNumber()));
2793 position.next();
2794 return;
2795 }
2796 ExtensionFunction extensionFunction = callTuple.getExtensionFunctions().get(awkName);
2797 if (extensionFunction != null) {
2798 resolveIndirectArguments(actualArguments, extensionFunction);
2799 if (profiling) {
2800 activeProfilingFunctions.push(new ActiveFunction(awkName, tupleStartNanos));
2801 }
2802 try {
2803 push(invokeExtension(extensionFunction, actualArguments, position.lineNumber()));
2804 } finally {
2805 if (profiling) {
2806 recordFunctionExit(System.nanoTime());
2807 }
2808 }
2809 position.next();
2810 return;
2811 }
2812 throw new AwkRuntimeException(position.lineNumber(), "function `" + qualifiedName + "' is not defined");
2813 }
2814
2815 private void execPrint(CountTuple tuple) throws IOException {
2816 long numArgs = tuple.getCount();
2817 if (numArgs == 0) {
2818 jrt.printRecordDefault();
2819 } else {
2820 jrt.printDefault(popArguments(numArgs));
2821 }
2822 }
2823
2824 private void execPrintToFile(CountAndAppendTuple tuple) throws IOException {
2825 String key = jrt.toAwkString(pop());
2826 long numArgs = tuple.getCount();
2827 jrt.printToFile(
2828 key,
2829 tuple.isAppend(),
2830 numArgs == 0 ? new Object[]
2831 { jrt.jrtGetInputField(0) } : popArguments(numArgs));
2832 }
2833
2834 private void execPrintToPipe(CountTuple tuple) throws IOException {
2835 String cmd = jrt.toAwkString(pop());
2836 long numArgs = tuple.getCount();
2837 jrt.printToProcess(cmd, numArgs == 0 ? new Object[] { jrt.jrtGetInputField(0) } : popArguments(numArgs));
2838 }
2839
2840 private void execPrintf(CountTuple tuple) throws IOException {
2841 long numArgs = tuple.getCount();
2842 Object[] values = popArguments(numArgs - 1);
2843 String format = jrt.toAwkString(pop());
2844 jrt.printfDefault(format, values);
2845 }
2846
2847 private void execPrintfToFile(CountAndAppendTuple tuple) throws IOException {
2848 String key = jrt.toAwkString(pop());
2849 long numArgs = tuple.getCount();
2850 Object[] values = popArguments(numArgs - 1);
2851 String format = jrt.toAwkString(pop());
2852 jrt.printfToFile(key, tuple.isAppend(), format, values);
2853 }
2854
2855 private void execPrintfToPipe(CountTuple tuple) throws IOException {
2856 String cmd = jrt.toAwkString(pop());
2857 long numArgs = tuple.getCount();
2858 Object[] values = popArguments(numArgs - 1);
2859 String format = jrt.toAwkString(pop());
2860 jrt.printfToProcess(cmd, format, values);
2861 }
2862
2863 private void execLength(CountTuple tuple) {
2864 long num = tuple.getCount();
2865 if (num == 0) {
2866 push(jrt.jrtGetInputField(0).toString().length());
2867 return;
2868 }
2869 Object value = pop();
2870 if (value instanceof ArgumentReference) {
2871 value = resolveLengthArgumentReference((ArgumentReference) value);
2872 }
2873 push(lengthOf(value));
2874 }
2875
2876 private Object lengthOf(Object value) {
2877 return value instanceof Map ?
2878 Long.valueOf(((Map<?, ?>) value).size()) :
2879 Integer.valueOf(jrt.toAwkString(value).length());
2880 }
2881
2882 private String normalizeIndirectFunctionName(String functionName) {
2883 return functionName.startsWith("awk::") ? functionName.substring("awk::".length()) : functionName;
2884 }
2885
2886
2887
2888
2889
2890
2891
2892
2893 private void resetCallState() {
2894 if (profiling) {
2895 recordAllFunctionExits(System.nanoTime());
2896 }
2897 runtimeStack.popAllFrames();
2898 elementArgumentReferences.clear();
2899 clearOperandStack();
2900 }
2901
2902
2903
2904
2905
2906
2907
2908 private void adoptElementArgumentReferences(Object[] actualArguments) {
2909 for (Object argument : actualArguments) {
2910 adoptElementArgumentReference(argument);
2911 }
2912 }
2913
2914 private void adoptElementArgumentReference(Object argument) {
2915 if (argument instanceof IndirectArrayArgumentReference) {
2916 IndirectArrayArgumentReference reference = (IndirectArrayArgumentReference) argument;
2917 if (reference.ownerFrame < 0) {
2918 reference.ownerFrame = runtimeStack.frameCount();
2919 elementArgumentReferences.push(reference);
2920 }
2921 }
2922 }
2923
2924
2925
2926
2927
2928
2929 private void releaseElementArgumentReferences() {
2930 int depth = runtimeStack.frameCount();
2931 while (!elementArgumentReferences.isEmpty() && elementArgumentReferences.peek().ownerFrame == depth) {
2932 elementArgumentReferences.pop();
2933 }
2934 }
2935
2936 private Object resolveUserFunctionArgument(Object argument) {
2937 if (!(argument instanceof ArgumentReference)) {
2938 return argument;
2939 }
2940 ArgumentReference reference = (ArgumentReference) argument;
2941 Object snapshot = reference.snapshot();
2942 if (snapshot instanceof ArgumentReference) {
2943 return resolveUserFunctionArgument(snapshot);
2944 }
2945 return isUntyped(snapshot) ? reference : snapshot;
2946 }
2947
2948 private void resolveIndirectArguments(Object[] actualArgumentsParam, BuiltinFunction builtin) {
2949 for (int index = 0; index < actualArgumentsParam.length; index++) {
2950 boolean arrayArgument = builtin == BuiltinFunction.SPLIT && index == 1;
2951 actualArgumentsParam[index] = resolveIndirectArgument(actualArgumentsParam[index], arrayArgument, false);
2952 }
2953 }
2954
2955 private void resolveIndirectArguments(Object[] actualArgumentsParam, ExtensionFunction function) {
2956 boolean[] arrayArguments = new boolean[actualArgumentsParam.length];
2957 boolean[] rawValueArguments = new boolean[actualArgumentsParam.length];
2958 for (int index : function.collectAssocArrayIndexes(actualArgumentsParam.length)) {
2959 arrayArguments[index] = true;
2960 }
2961 for (int index : function.collectRawValueIndexes(actualArgumentsParam.length)) {
2962 rawValueArguments[index] = true;
2963 }
2964 for (int index = 0; index < actualArgumentsParam.length; index++) {
2965 actualArgumentsParam[index] = resolveIndirectArgument(
2966 actualArgumentsParam[index],
2967 arrayArguments[index],
2968 rawValueArguments[index]);
2969 }
2970 }
2971
2972 private Object resolveIndirectArgument(Object argument, boolean arrayArgument, boolean rawValueArgument) {
2973 if (!(argument instanceof ArgumentReference)) {
2974 return argument;
2975 }
2976 ArgumentReference reference = (ArgumentReference) argument;
2977 if (rawValueArgument) {
2978 return resolveRawArgumentReference(reference);
2979 }
2980 return resolveArgumentReference(reference, arrayArgument);
2981 }
2982
2983 private Object invokeIndirectBuiltin(BuiltinFunction builtin, Object[] args, int lineNumber) {
2984 switch (builtin) {
2985 case ATAN2:
2986 requireIndirectArgumentCount(builtin, args, 2, 2, lineNumber);
2987 return Math.atan2(JRT.toDouble(args[0]), JRT.toDouble(args[1]));
2988 case CLOSE:
2989 requireIndirectArgumentCount(builtin, args, 1, 1, lineNumber);
2990 return jrt.jrtClose(jrt.toAwkString(args[0]));
2991 case COS:
2992 requireIndirectArgumentCount(builtin, args, 1, 1, lineNumber);
2993 return Math.cos(JRT.toDouble(args[0]));
2994 case EXP:
2995 requireIndirectArgumentCount(builtin, args, 1, 1, lineNumber);
2996 return Math.exp(JRT.toDouble(args[0]));
2997 case GSUB:
2998 case SUB:
2999 requireIndirectArgumentCount(builtin, args, 2, 2, lineNumber);
3000 return substituteInputLine(builtin == BuiltinFunction.GSUB, args[0], args[1]);
3001 case INDEX:
3002 requireIndirectArgumentCount(builtin, args, 2, 2, lineNumber);
3003 return jrt.index(jrt.toAwkString(args[0]), jrt.toAwkString(args[1]));
3004 case INT:
3005 requireIndirectArgumentCount(builtin, args, 1, 1, lineNumber);
3006 return JRT.truncateToScalar(JRT.toDouble(args[0]));
3007 case LENGTH:
3008 requireIndirectArgumentCount(builtin, args, 0, 1, lineNumber);
3009 return args.length == 0 ? Integer.valueOf(jrt.jrtGetInputField(0).toString().length()) : lengthOf(args[0]);
3010 case LOG:
3011 requireIndirectArgumentCount(builtin, args, 1, 1, lineNumber);
3012 return Math.log(JRT.toDouble(args[0]));
3013 case MATCH:
3014 requireIndirectArgumentCount(builtin, args, 2, 2, lineNumber);
3015 return jrt.matchPosition(jrt.toAwkString(args[0]), jrt.toAwkString(args[1]));
3016 case RAND:
3017 requireIndirectArgumentCount(builtin, args, 0, 0, lineNumber);
3018 return randomNumberGenerator.nextDouble();
3019 case SIN:
3020 requireIndirectArgumentCount(builtin, args, 1, 1, lineNumber);
3021 return Math.sin(JRT.toDouble(args[0]));
3022 case SPLIT:
3023 requireIndirectArgumentCount(builtin, args, 2, 3, lineNumber);
3024 return splitIntoArray(args[0], args[1], args.length == 3 ? args[2] : jrt.getFSVar(), lineNumber);
3025 case SPRINTF:
3026 requireIndirectArgumentCount(builtin, args, 1, Integer.MAX_VALUE, lineNumber);
3027 return jrt.sprintf(jrt.toAwkString(args[0]), Arrays.copyOfRange(args, 1, args.length));
3028 case SQRT:
3029 requireIndirectArgumentCount(builtin, args, 1, 1, lineNumber);
3030 return Math.sqrt(JRT.toDouble(args[0]));
3031 case SRAND:
3032 requireIndirectArgumentCount(builtin, args, 0, 1, lineNumber);
3033 int seed = args.length == 0 ? JRT.timeSeed() : (int) JRT.toDouble(args[0]);
3034 int previousSeed = randomNumberGenerator.getSeed();
3035 randomNumberGenerator.setSeed(seed);
3036 return Integer.valueOf(previousSeed);
3037 case SUBSTR:
3038 requireIndirectArgumentCount(builtin, args, 2, 3, lineNumber);
3039 return substring(args[0], args[1], args.length == 3 ? args[2] : null);
3040 case SYSTEM:
3041 requireIndirectArgumentCount(builtin, args, 1, 1, lineNumber);
3042 return jrt.jrtSystem(jrt.toAwkString(args[0]));
3043 case TOLOWER:
3044 requireIndirectArgumentCount(builtin, args, 1, 1, lineNumber);
3045 return jrt.toAwkString(args[0]).toLowerCase();
3046 case TOUPPER:
3047 requireIndirectArgumentCount(builtin, args, 1, 1, lineNumber);
3048 return jrt.toAwkString(args[0]).toUpperCase();
3049 default:
3050 throw new AwkRuntimeException(
3051 lineNumber,
3052 "indirect calls are not implemented for builtin `" + builtin.getAwkName() + "'");
3053 }
3054 }
3055
3056 private void requireIndirectArgumentCount(
3057 BuiltinFunction builtin,
3058 Object[] args,
3059 int minimum,
3060 int maximum,
3061 int lineNumber) {
3062 if (args.length < minimum || args.length > maximum) {
3063 String expected = minimum == maximum ? Integer.toString(minimum) : minimum + " to " + maximum;
3064 throw new AwkRuntimeException(
3065 lineNumber,
3066 builtin.getAwkName() + " requires " + expected + " argument(s), not " + args.length);
3067 }
3068 }
3069
3070 private Object invokeExtension(ExtensionFunction function, Object[] args, int lineNumber) {
3071
3072 currentLineNumber = lineNumber;
3073 String extensionClassName = function.getExtensionClassName();
3074 JawkExtension extension = extensionInstances.get(extensionClassName);
3075 if (extension == null) {
3076 throw new AwkRuntimeException(
3077 lineNumber,
3078 "Extension instance for class '" + extensionClassName + "' is not registered");
3079 }
3080 if (!(extension instanceof AbstractExtension)) {
3081 throw new AwkRuntimeException(
3082 lineNumber,
3083 "Extension instance for class '" + extensionClassName + "' does not extend "
3084 + AbstractExtension.class.getName());
3085 }
3086 Map<IndirectArrayArgumentReference, Object> attachedValues = captureAttachedArrayArgumentValues();
3087 Object result;
3088 try {
3089 result = function.invoke((AbstractExtension) extension, args);
3090 } finally {
3091 detachReplacedArrayArgumentReferences(attachedValues);
3092 }
3093 if (result == null) {
3094 return "";
3095 }
3096 if (result instanceof Number || result instanceof String || result instanceof Map) {
3097 return result;
3098 }
3099 return jrt.toAwkString(result);
3100 }
3101
3102 private void execMatch() {
3103 String ere = jrt.toAwkString(pop());
3104 String s = jrt.toAwkString(pop());
3105 push(jrt.matchPosition(s, ere));
3106 }
3107
3108 private void execSubForDollar0(BooleanTuple tuple) {
3109 Object replacement = pop();
3110 Object ere = pop();
3111 push(substituteInputLine(tuple.getValue(), ere, replacement));
3112 }
3113
3114 private Object substituteInputLine(boolean global, Object ere, Object replacement) {
3115 String orig = jrt.toAwkString(jrt.jrtGetInputField(0));
3116 Object replacements = global ?
3117 jrt.replaceAll(orig, jrt.toAwkString(replacement), jrt.toAwkString(ere)) :
3118 jrt.replaceFirst(orig, jrt.toAwkString(replacement), jrt.toAwkString(ere));
3119 jrt.setInputLine(jrt.getReplaceResult());
3120 jrt.jrtParseFields();
3121 return replacements;
3122 }
3123
3124 private void execSubForDollarReference(BooleanTuple tuple) {
3125 boolean isGsub = tuple.getValue();
3126 long fieldNum = JRT.parseFieldNumber(pop());
3127 String orig = jrt.toAwkString(pop());
3128 String repl = jrt.toAwkString(pop());
3129 String ere = jrt.toAwkString(pop());
3130 push(isGsub ? jrt.replaceAll(orig, repl, ere) : jrt.replaceFirst(orig, repl, ere));
3131 String newstring = jrt.getReplaceResult();
3132 if (fieldNum == 0) {
3133 jrt.setInputLine(newstring);
3134 jrt.jrtParseFields();
3135 } else {
3136 jrt.jrtSetInputField(newstring, fieldNum);
3137 }
3138 }
3139
3140 private void execSubForVariable(SubstitutionVariableTuple tuple, PositionTracker position) {
3141 String newString = execSubOrGSub(tuple.isGlobalSubstitution());
3142 assign(tuple.getVariableOffset(), newString, tuple.isGlobal(), position, false);
3143 }
3144
3145 private void execSubForArrayReference(SubstitutionVariableTuple tuple) {
3146 Object arrIdx = pop();
3147 String newString = execSubOrGSub(tuple.isGlobalSubstitution());
3148 assignArray(tuple.getVariableOffset(), arrIdx, newString, tuple.isGlobal());
3149 pop();
3150 }
3151
3152 private void execSubForMapReference(BooleanTuple tuple) {
3153 Object arrIdx = pop();
3154 Map<Object, Object> array = toMap(pop());
3155 String newString = execSubOrGSub(tuple.getValue());
3156 assignMapElement(array, arrIdx, newString);
3157 pop();
3158 }
3159
3160 private void execSplit(CountTuple tuple, PositionTracker position) {
3161 long numArgs = tuple.getCount();
3162 Object fs;
3163 if (numArgs == 2) {
3164 fs = jrt.getFSVar();
3165 } else if (numArgs == 3) {
3166
3167
3168 fs = pop();
3169 } else {
3170 throw new Error("Invalid # of args. split() requires 2 or 3. Got: " + numArgs);
3171 }
3172 Object target = pop();
3173 Object source = pop();
3174 push(splitIntoArray(source, target, fs, position.lineNumber()));
3175 }
3176
3177 private Object splitIntoArray(Object source, Object target, Object separator, int lineNumber) {
3178 if (!(target instanceof Map)) {
3179 throw new AwkRuntimeException(lineNumber, target + " is not an array.");
3180 }
3181 Enumeration<Object> tokenizer = jrt.splitTokenizer(jrt.toAwkString(source), separator);
3182 @SuppressWarnings("unchecked")
3183 Map<Object, Object> assocArray = (Map<Object, Object>) target;
3184 assocArray.clear();
3185 detachMissingArrayArgumentReferences(assocArray);
3186 long cnt = 0;
3187 while (tokenizer.hasMoreElements()) {
3188 Object value = tokenizer.nextElement();
3189 assocArray.put(++cnt, jrt.toInputScalar(value));
3190 }
3191 return Long.valueOf(cnt);
3192 }
3193
3194 private void execSubstr(CountTuple tuple) {
3195 long numArgs = tuple.getCount();
3196 Object length = null;
3197 if (numArgs == 3) {
3198 length = pop();
3199 } else if (numArgs != 2) {
3200 throw new Error("numArgs for SUBSTR must be 2 or 3. It is " + numArgs);
3201 }
3202 Object start = pop();
3203 Object value = pop();
3204 push(substring(value, start, length));
3205 }
3206
3207 private Object substring(Object value, Object start, Object requestedLength) {
3208 String s = jrt.toAwkString(value);
3209 int startPos = (int) JRT.toDouble(start);
3210 if (startPos <= 0) {
3211 startPos = 1;
3212 }
3213 if (startPos > s.length()) {
3214 return BLANK;
3215 }
3216 int available = s.length() - startPos + 1;
3217
3218
3219
3220 long length = requestedLength == null ? available : JRT.toLong(requestedLength);
3221 if (length <= 0) {
3222 return BLANK;
3223 }
3224 if (length > available) {
3225 length = available;
3226 }
3227 return s.substring(startPos - 1, startPos - 1 + (int) length);
3228 }
3229
3230 private void execSetNumGlobals(CountTuple tuple) {
3231 long numGlobals = tuple.getCount();
3232 Object[] globals = runtimeStack.getNumGlobals();
3233 if (mergedGlobalLayoutActive) {
3234 if (!hasCompatiblePersistentGlobalLayout(numGlobals)) {
3235 throw new IllegalStateException("AVM globals are already initialized for an incompatible persistent layout.");
3236 }
3237 applyExecutionInitialVariablesToGlobalSlots(true);
3238 } else if (globals == null) {
3239 runtimeStack.setNumGlobals(numGlobals, globalVariableOffsets);
3240 initializedEvalGlobalVariableOffsets = globalVariableOffsets;
3241 initializedEvalGlobalVariableArrays = globalVariableArrays;
3242 applyExecutionInitialVariablesToGlobalSlots(false);
3243 } else if (!hasCompatibleEvalGlobalLayout(numGlobals)) {
3244 throw new IllegalStateException(
3245 "AVM globals are already initialized for a different eval layout. Call prepareForEval(...) first.");
3246 }
3247 }
3248
3249 private void populateEnviron(long offset) {
3250 environOffset = offset;
3251 for (Map.Entry<String, String> var : System.getenv().entrySet()) {
3252 assignArray(environOffset, var.getKey(), jrt.toInputScalar(var.getValue()), true);
3253 pop();
3254 }
3255 }
3256
3257 private void populateArgc(long offset) {
3258 argcOffset = offset;
3259
3260 runtimeStack.setVariable(argcOffset, Integer.valueOf(arguments.size() + 1), true);
3261 }
3262
3263 private void populateArgv(long offset) {
3264 argvOffset = offset;
3265
3266
3267 Object existing = runtimeStack.getVariable(argvOffset, true);
3268 if (existing instanceof Map && !((Map<?, ?>) existing).isEmpty()) {
3269 return;
3270 }
3271 forEachArgvEntry((index, value) -> {
3272 assignArray(argvOffset, index, value, true);
3273 pop();
3274 });
3275 }
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286 private void forEachArgvEntry(BiConsumer<Long, Object> consumer) {
3287 consumer.accept(Long.valueOf(0L), "jawk");
3288 for (int i = 1; i <= arguments.size(); i++) {
3289 consumer.accept(Long.valueOf(i), jrt.toInputScalar(arguments.get(i - 1)));
3290 }
3291 }
3292
3293
3294
3295
3296
3297
3298 private void runBeforeStartHooks() {
3299 if (beforeStartHooksExecuted || extensionInstances.isEmpty()) {
3300 return;
3301 }
3302 beforeStartHooksExecuted = true;
3303 Set<JawkExtension> started = new LinkedHashSet<JawkExtension>();
3304 for (JawkExtension extension : extensionInstances.values()) {
3305 if (started.add(extension)) {
3306 extension.beforeStart(this, jrt);
3307 }
3308 }
3309 }
3310
3311
3312
3313
3314
3315
3316
3317
3318 private void execUpdateSymtab(long offset) {
3319 symtabOffset = offset;
3320 if (runtimeStack.getVariable(offset, true) != null) {
3321
3322 return;
3323 }
3324 SymtabArray symtab = new SymtabArray();
3325 for (String name : executionInitialVariables.keySet()) {
3326 symtab.put(name, getVariable(name));
3327 }
3328 for (String name : getGlobalVariableNames()) {
3329
3330 if ("SYMTAB".equals(name) || "FUNCTAB".equals(name)) {
3331 continue;
3332 }
3333 symtab.put(name, getVariable(name));
3334 }
3335
3336
3337 for (String name : getSpecialVariableNames()) {
3338 symtab.put(name, getVariable(name));
3339 }
3340 symtab.activate();
3341 runtimeStack.setVariable(offset, symtab, true);
3342 }
3343
3344 private final class SymtabArray extends java.util.AbstractMap<Object, Object> implements AssocArray {
3345 private final Map<Object, Object> entries = newAwkArray();
3346 private final Set<String> assignableNames = new HashSet<String>();
3347 private boolean active;
3348
3349 private void activate() {
3350 active = true;
3351 }
3352
3353
3354 @Override
3355 public Object get(Object key) {
3356 String name = key == null ? "" : key.toString();
3357 if (active && !isMetaTableName(name) && isLiveSpecialVariable(name)) {
3358 return getVariable(name);
3359 }
3360 Integer offset = globalVariableOffsets == null ? null : globalVariableOffsets.get(name);
3361 if (active && !isMetaTableName(name) && offset != null) {
3362 return runtimeStack.getVariable(offset.intValue(), true);
3363 }
3364 return entries.get(key);
3365 }
3366
3367
3368 @Override
3369 public boolean containsKey(Object key) {
3370 String name = key == null ? "" : key.toString();
3371 return active
3372 && !isMetaTableName(name)
3373 && (isLiveSpecialVariable(name) || (globalVariableOffsets != null && globalVariableOffsets.containsKey(name)))
3374 || entries.containsKey(key);
3375 }
3376
3377
3378 @Override
3379 public Object put(Object key, Object value) {
3380 String name = key == null ? "" : key.toString();
3381 if (!active) {
3382 if (key != null) {
3383 assignableNames.add(name);
3384 }
3385 return entries.put(key, value);
3386 }
3387 if (!assignableNames.contains(name)) {
3388 throw new AwkRuntimeException("Cannot assign to an arbitrary element of SYMTAB.");
3389 }
3390 validateGlobalType(name, value);
3391 Object previous = entries.put(key, value);
3392 if (isMetaTableName(name)) {
3393 return previous;
3394 }
3395 if (applyLiveSpecialVariable(name, value)) {
3396 return previous;
3397 }
3398 Integer offset = globalVariableOffsets == null ? null : globalVariableOffsets.get(name);
3399 if (offset != null) {
3400 runtimeStack.setVariable(offset.intValue(), value, true);
3401 }
3402 return previous;
3403 }
3404
3405 private Object putRuntimeVariable(String name, Object value) {
3406 assignableNames.add(name);
3407 return put(name, value);
3408 }
3409
3410 private boolean applyLiveSpecialVariable(String name, Object value) {
3411 if ("ARGC".equals(name)) {
3412 if (argcOffset == NULL_OFFSET) {
3413 throw new AwkRuntimeException("ARGC is read-only (not materialized).");
3414 }
3415 runtimeStack.setVariable(argcOffset, value, true);
3416 return true;
3417 }
3418 if ("RSTART".equals(name)) {
3419 jrt.setRSTART(value);
3420 return true;
3421 }
3422 if ("RLENGTH".equals(name)) {
3423 jrt.setRLENGTH(value);
3424 return true;
3425 }
3426 return isManagedSpecialVariable(name) && jrt.applySpecialVariable(name, value);
3427 }
3428
3429 private boolean isLiveSpecialVariable(String name) {
3430 return isManagedSpecialVariable(name) || "RSTART".equals(name) || "RLENGTH".equals(name);
3431 }
3432
3433
3434 @Override
3435 public Object remove(Object key) {
3436 if (active) {
3437 throw new AwkRuntimeException("Cannot delete an element from SYMTAB.");
3438 }
3439 return entries.remove(key);
3440 }
3441
3442
3443 @Override
3444 public void clear() {
3445 if (active) {
3446 throw new AwkRuntimeException("Cannot delete SYMTAB.");
3447 }
3448 entries.clear();
3449 }
3450
3451 private void validateGlobalType(String name, Object value) {
3452 Boolean array = globalVariableArrays == null ? null : globalVariableArrays.get(name);
3453 if (Boolean.TRUE.equals(array) && !(value instanceof Map)) {
3454 throw new AwkRuntimeException("Attempting to use array `" + name + "' in a scalar context.");
3455 }
3456 if (Boolean.FALSE.equals(array) && value instanceof Map) {
3457 throw new AwkRuntimeException("Attempting to use scalar `" + name + "' as an array.");
3458 }
3459 }
3460
3461
3462 @Override
3463 public Set<Map.Entry<Object, Object>> entrySet() {
3464 return new AbstractSet<Map.Entry<Object, Object>>() {
3465
3466 @Override
3467 public Iterator<Map.Entry<Object, Object>> iterator() {
3468 final Iterator<Map.Entry<Object, Object>> iterator = entries.entrySet().iterator();
3469 return new Iterator<Map.Entry<Object, Object>>() {
3470
3471 @Override
3472 public boolean hasNext() {
3473 return iterator.hasNext();
3474 }
3475
3476 @Override
3477 public Map.Entry<Object, Object> next() {
3478 return new LiveSymtabEntry(iterator.next().getKey());
3479 }
3480
3481 @Override
3482 public void remove() {
3483 if (active) {
3484 throw new AwkRuntimeException("Cannot delete an element from SYMTAB.");
3485 }
3486 iterator.remove();
3487 }
3488 };
3489 }
3490
3491 @Override
3492 public int size() {
3493 return entries.size();
3494 }
3495 };
3496 }
3497
3498 private boolean isMetaTableName(String name) {
3499 return "SYMTAB".equals(name) || "FUNCTAB".equals(name);
3500 }
3501
3502 private final class LiveSymtabEntry implements Map.Entry<Object, Object> {
3503
3504 private final Object key;
3505
3506 private LiveSymtabEntry(Object key) {
3507 this.key = key;
3508 }
3509
3510 @Override
3511 public Object getKey() {
3512 return key;
3513 }
3514
3515 @Override
3516 public Object getValue() {
3517 return SymtabArray.this.get(key);
3518 }
3519
3520 @Override
3521 public Object setValue(Object value) {
3522 return SymtabArray.this.put(key, value);
3523 }
3524
3525 @Override
3526 public boolean equals(Object obj) {
3527 if (!(obj instanceof Map.Entry)) {
3528 return false;
3529 }
3530 Map.Entry<?, ?> other = (Map.Entry<?, ?>) obj;
3531 return Objects.equals(key, other.getKey()) && Objects.equals(getValue(), other.getValue());
3532 }
3533
3534 @Override
3535 public int hashCode() {
3536 return Objects.hashCode(key) ^ Objects.hashCode(getValue());
3537 }
3538 }
3539 }
3540
3541
3542
3543
3544
3545
3546
3547 private void execUpdateFunctab(long offset) {
3548 if (runtimeStack.getVariable(offset, true) != null) {
3549 return;
3550 }
3551 Map<Object, Object> functab = newAwkArray();
3552 for (String name : BuiltinFunction.names()) {
3553 functab.put(name, name);
3554 }
3555 for (String name : functionNames) {
3556 functab.put(name, name);
3557 }
3558 Set<JawkExtension> seen = new LinkedHashSet<JawkExtension>();
3559 for (JawkExtension extension : extensionInstances.values()) {
3560 if (seen.add(extension)) {
3561 for (String keyword : extension.getExtensionFunctions().keySet()) {
3562 functab.put(keyword, keyword);
3563 }
3564 }
3565 }
3566 runtimeStack.setVariable(offset, new ReadOnlyArray("FUNCTAB", functab), true);
3567 }
3568
3569 private static final class ReadOnlyArray extends java.util.AbstractMap<Object, Object> implements AssocArray {
3570 private final String name;
3571 private final Map<Object, Object> entries;
3572
3573 private ReadOnlyArray(String nameParam, Map<Object, Object> entriesParam) {
3574 name = nameParam;
3575 entries = entriesParam;
3576 }
3577
3578
3579 @Override
3580 public Object get(Object key) {
3581 return JRT.containsAwkKey(entries, key) ? entries.get(key) : BLANK;
3582 }
3583
3584
3585 @Override
3586 public boolean containsKey(Object key) {
3587 return JRT.containsAwkKey(entries, key);
3588 }
3589
3590
3591 @Override
3592 public Set<Map.Entry<Object, Object>> entrySet() {
3593 return Collections.unmodifiableMap(entries).entrySet();
3594 }
3595
3596
3597 @Override
3598 public Object put(Object key, Object value) {
3599 throw readOnlyError();
3600 }
3601
3602
3603 @Override
3604 public Object remove(Object key) {
3605 throw readOnlyError();
3606 }
3607
3608
3609 @Override
3610 public void clear() {
3611 throw readOnlyError();
3612 }
3613
3614 private AwkRuntimeException readOnlyError() {
3615 return new AwkRuntimeException(name + " is read-only.");
3616 }
3617 }
3618
3619
3620 private void updateSymtabEntry(String name, Object value) {
3621 if (symtabOffset == NULL_OFFSET) {
3622 return;
3623 }
3624 Object symtab = runtimeStack.getVariable(symtabOffset, true);
3625 if (symtab instanceof SymtabArray) {
3626 ((SymtabArray) symtab).putRuntimeVariable(name, value);
3627 } else if (symtab instanceof Map) {
3628 @SuppressWarnings("unchecked")
3629 Map<Object, Object> symtabMap = (Map<Object, Object>) symtab;
3630 symtabMap.put(name, value);
3631 }
3632 }
3633
3634 private void execApplySubsep(CountTuple tuple) {
3635 long count = tuple.getCount();
3636 if (count == 1) {
3637 Object value = pop();
3638 checkScalar(value);
3639
3640
3641
3642
3643
3644 push(AssocArray.isIntegralNumberKey(value) ? value : jrt.toAwkString(value));
3645 return;
3646 }
3647 StringBuilder sb = new StringBuilder();
3648 Object value = pop();
3649 checkScalar(value);
3650 sb.append(jrt.toAwkString(value));
3651 String subsep = jrt.toAwkString(jrt.getSUBSEPVar());
3652 for (int i = 1; i < count; i++) {
3653 sb.insert(0, subsep);
3654 value = pop();
3655 checkScalar(value);
3656 sb.insert(0, jrt.toAwkString(value));
3657 }
3658 push(sb.toString());
3659 }
3660
3661 private long beforeProfiledTuple(Tuple tuple, Opcode opcode) {
3662 long now = System.nanoTime();
3663 if (opcode == Opcode.CALL_FUNCTION) {
3664 CallFunctionTuple callTuple = (CallFunctionTuple) tuple;
3665 activeProfilingFunctions.push(new ActiveFunction(callTuple.getFunctionName(), now));
3666 } else if (opcode == Opcode.EXTENSION) {
3667 ExtensionTuple extensionTuple = (ExtensionTuple) tuple;
3668 ExtensionFunction function = extensionTuple.getFunction();
3669 activeProfilingFunctions.push(new ActiveFunction(function.getKeyword(), now));
3670 }
3671 return now;
3672 }
3673
3674 private void afterProfiledTuple(Opcode opcode, long tupleStartNanos) {
3675 long now = System.nanoTime();
3676 statisticsFor(tupleProfilingStats, opcode).add(now - tupleStartNanos);
3677 if (opcode == Opcode.EXIT_WITH_CODE || opcode == Opcode.EXIT_WITHOUT_CODE) {
3678 recordAllFunctionExits(now);
3679 } else if (opcode == Opcode.EXTENSION || opcode == Opcode.RETURN_FROM_FUNCTION) {
3680 recordFunctionExit(now);
3681 }
3682 }
3683
3684 private static <K> ProfilingReport.Accumulator statisticsFor(Map<K, ProfilingReport.Accumulator> stats, K key) {
3685 ProfilingReport.Accumulator accumulator = stats.get(key);
3686 if (accumulator == null) {
3687 accumulator = new ProfilingReport.Accumulator();
3688 stats.put(key, accumulator);
3689 }
3690 return accumulator;
3691 }
3692
3693 private void recordFunctionExit(long now) {
3694 if (activeProfilingFunctions.isEmpty()) {
3695 return;
3696 }
3697 ActiveFunction function = activeProfilingFunctions.pop();
3698 statisticsFor(functionProfilingStats, function.name).add(now - function.startNanos);
3699 }
3700
3701 private void recordAllFunctionExits(long now) {
3702 while (!activeProfilingFunctions.isEmpty()) {
3703 recordFunctionExit(now);
3704 }
3705 }
3706
3707 private static final class ActiveFunction {
3708 private final String name;
3709 private final long startNanos;
3710
3711 private ActiveFunction(String name, long startNanos) {
3712 this.name = name;
3713 this.startNanos = startNanos;
3714 }
3715 }
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728 @Override
3729 public void close() throws IOException {
3730 jrt.jrtCloseAll();
3731 closeResolvedInputSource();
3732 resolvedInputSource = null;
3733 }
3734
3735
3736
3737
3738
3739
3740
3741 private void closeResolvedInputSource() {
3742 closeInputSource(resolvedInputSource);
3743 }
3744
3745 private void closeInputSource(InputSource inputSource) {
3746 if (!(inputSource instanceof Closeable)) {
3747 return;
3748 }
3749 try {
3750 ((Closeable) inputSource).close();
3751 } catch (IOException ignored) {
3752
3753 }
3754 }
3755
3756 private Object[] popArguments(long numArgs) {
3757 int count = (int) numArgs;
3758 Object[] args = new Object[count];
3759 operandStackSize -= count;
3760 System.arraycopy(operandStack, operandStackSize, args, 0, count);
3761 Arrays.fill(operandStack, operandStackSize, operandStackSize + count, null);
3762 return args;
3763 }
3764
3765
3766
3767
3768 private String sprintfFunction(long numArgs) {
3769 Object[] argArray = popArguments(numArgs - 1);
3770 String fmt = jrt.toAwkString(pop());
3771 return jrt.sprintf(fmt, argArray);
3772 }
3773
3774 private void setNumOnJRT(long fieldNum, Object num) {
3775
3776
3777
3778 if (fieldNum == 0) {
3779 jrt.setInputLine(jrt.toAwkString(num));
3780 jrt.jrtParseFields();
3781 } else {
3782 jrt.jrtSetInputField(num, fieldNum);
3783 }
3784 }
3785
3786 private String execSubOrGSub(boolean isGsub) {
3787 String newString;
3788
3789
3790
3791
3792 String orig = jrt.toAwkString(pop());
3793 String repl = jrt.toAwkString(pop());
3794 String ere = jrt.toAwkString(pop());
3795 push(isGsub ? jrt.replaceAll(orig, repl, ere) : jrt.replaceFirst(orig, repl, ere));
3796 newString = jrt.getReplaceResult();
3797
3798 return newString;
3799 }
3800
3801
3802
3803
3804 private void assign(long l, Object value, boolean isGlobal, PositionTracker position, boolean push) {
3805 value = JRT.untypedToBlank(value);
3806 checkScalar(value);
3807
3808 if (resolveVariable(l, isGlobal, false) instanceof Map) {
3809 throw new AwkRuntimeException(position.lineNumber(), "Attempting to use an array in a scalar context.");
3810 }
3811 if (push) {
3812 push(value);
3813 }
3814 runtimeStack.setVariable(l, value, isGlobal);
3815
3816 }
3817
3818
3819
3820
3821 private void assignArray(long offset, Object arrIdx, Object rhs, boolean isGlobal) {
3822 assignMapElement(ensureMapVariable(offset, isGlobal), arrIdx, rhs);
3823 }
3824
3825 private void assignMapElement(Map<Object, Object> array, Object arrIdx, Object rhs) {
3826 checkScalar(arrIdx);
3827 rhs = JRT.untypedToBlank(rhs);
3828 checkScalar(rhs);
3829 if (JRT.containsAwkKey(array, arrIdx) && JRT.getAssocArrayValue(array, arrIdx) instanceof Map) {
3830 throw new AwkRuntimeException("Attempting to use an array in a scalar context.");
3831 }
3832 array.put(arrIdx, rhs);
3833 push(rhs);
3834 }
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845 private static Object blankToZero(Object o) {
3846 return o == null || o instanceof UninitializedObject ? ZERO : o;
3847 }
3848
3849
3850
3851
3852
3853 private static Object numericValueOf(Object o) {
3854 if (o instanceof Long || o instanceof Integer || o instanceof Double) {
3855 return o;
3856 }
3857 return JRT.toDouble(o);
3858 }
3859
3860 private Object inc(long l, boolean isGlobal) {
3861 Object o = resolveVariable(l, isGlobal, false);
3862 if (o instanceof UninitializedObject) {
3863 o = ZERO;
3864 runtimeStack.setVariable(l, o, isGlobal);
3865 }
3866 Object updated = JRT.inc(o);
3867 runtimeStack.setVariable(l, updated, isGlobal);
3868 return o;
3869 }
3870
3871
3872
3873
3874
3875 private Object dec(long l, boolean isGlobal) {
3876 Object o = resolveVariable(l, isGlobal, false);
3877 if (o instanceof UninitializedObject) {
3878 o = ZERO;
3879 runtimeStack.setVariable(l, o, isGlobal);
3880 }
3881 Object updated = JRT.dec(o);
3882 runtimeStack.setVariable(l, updated, isGlobal);
3883 return o;
3884 }
3885
3886
3887 @Override
3888 public final Object getRS() {
3889 return jrt.getRSVar();
3890 }
3891
3892
3893 @Override
3894 public final Object getOFS() {
3895 return jrt.getOFSVar();
3896 }
3897
3898
3899 @Override
3900 public final Object getORS() {
3901 return jrt.getORSVar();
3902 }
3903
3904
3905 @Override
3906 public final Object getSUBSEP() {
3907 return jrt.getSUBSEPVar();
3908 }
3909
3910
3911
3912
3913
3914
3915
3916
3917 public Set<String> getGlobalVariableNames() {
3918 return globalVariableOffsets == null ?
3919 Collections.<String>emptySet() :
3920 Collections.unmodifiableSet(globalVariableOffsets.keySet());
3921 }
3922
3923
3924
3925
3926
3927
3928
3929 public Set<String> getFunctionNames() {
3930 return functionNames == null ? Collections.<String>emptySet() : Collections.unmodifiableSet(functionNames);
3931 }
3932
3933
3934
3935
3936
3937
3938 private static final Set<String> SPECIAL_VARIABLE_NAMES = Collections.unmodifiableSet(
3939 new LinkedHashSet<String>(
3940 Arrays.asList(
3941 "FS",
3942 "RS",
3943 "OFS",
3944 "ORS",
3945 "FILENAME",
3946 "SUBSEP",
3947 "CONVFMT",
3948 "OFMT",
3949 "NF",
3950 "NR",
3951 "FNR",
3952 "RSTART",
3953 "RLENGTH",
3954 "IGNORECASE",
3955 "ERRNO",
3956 "ARGIND",
3957 "ARGC",
3958 "ARGV")));
3959
3960
3961
3962
3963
3964
3965
3966 public Set<String> getSpecialVariableNames() {
3967 return SPECIAL_VARIABLE_NAMES;
3968 }
3969
3970
3971 @Override
3972 public final Object getVariable(String name) {
3973 if (name == null) {
3974 return null;
3975 }
3976 switch (name) {
3977 case "FS":
3978 return getFS();
3979 case "RS":
3980 return getRS();
3981 case "OFS":
3982 return getOFS();
3983 case "ORS":
3984 return getORS();
3985 case "FILENAME":
3986 return jrt.getFILENAME();
3987 case "SUBSEP":
3988 return getSUBSEP();
3989 case "CONVFMT":
3990 return getCONVFMT();
3991 case "OFMT":
3992 return jrt.getOFMTString();
3993 case "NF":
3994 return jrt.getNF();
3995 case "NR":
3996 return jrt.getNR();
3997 case "FNR":
3998 return jrt.getFNR();
3999 case "RSTART":
4000 return jrt.getRSTART();
4001 case "RLENGTH":
4002 return jrt.getRLENGTH();
4003 case "IGNORECASE":
4004 return jrt.getIGNORECASEVar();
4005 case "ERRNO":
4006 if (isManagedSpecialVariable(name)) {
4007 return jrt.getERRNO();
4008 }
4009
4010 break;
4011 case "ARGIND":
4012 if (isManagedSpecialVariable(name)) {
4013 return jrt.getARGIND();
4014 }
4015
4016 break;
4017
4018 case "ARGC":
4019 return getARGC();
4020 case "ARGV":
4021 return getARGV();
4022 default:
4023 break;
4024 }
4025 if (globalVariableOffsets == null) {
4026 return executionInitialVariables.get(name);
4027 }
4028 Integer offsetObj = globalVariableOffsets.get(name);
4029 if (offsetObj != null) {
4030 return runtimeStack.getVariable(offsetObj.intValue(), true);
4031 }
4032
4033
4034
4035 return executionInitialVariables == null ? null : executionInitialVariables.get(name);
4036 }
4037
4038
4039
4040
4041
4042
4043
4044 public String getSourceDescription() {
4045 return sourceDescription;
4046 }
4047
4048
4049
4050
4051
4052
4053
4054 public int getCurrentLineNumber() {
4055 return currentLineNumber;
4056 }
4057
4058
4059 @Override
4060 public final void assignVariable(String name, Object obj) {
4061
4062
4063 if (globalVariableOffsets == null || globalVariableArrays == null) {
4064 Object normalized = normalizeExternalVariableValue(obj);
4065 baseInitialVariables.put(name, normalized);
4066 if (isManagedSpecialVariable(name)) {
4067 baseSpecialVariables.put(name, normalized);
4068 }
4069 return;
4070 }
4071
4072
4073 if (functionNames.contains(name)) {
4074 throw new IllegalArgumentException("Cannot assign a scalar to a function name (" + name + ").");
4075 }
4076
4077 Object normalized = normalizeExternalVariableValue(obj);
4078
4079
4080
4081
4082 if (!"ARGC".equals(name) && isManagedSpecialVariable(name)) {
4083 jrt.applySpecialVariable(name, normalized);
4084 updateSymtabEntry(name, normalized);
4085 return;
4086 }
4087
4088 Integer offsetObj = globalVariableOffsets.get(name);
4089 Boolean arrayObj = globalVariableArrays.get(name);
4090
4091 if (offsetObj != null) {
4092 if (arrayObj.booleanValue() && !(normalized instanceof Map)) {
4093 throw new IllegalArgumentException("Cannot assign a scalar to a non-scalar variable (" + name + ").");
4094 }
4095 runtimeStack.setFilelistVariable(offsetObj.intValue(), normalized);
4096 } else if (runtimeStack.hasGlobalVariable(name)) {
4097 runtimeStack.setGlobalVariable(name, normalized);
4098 }
4099
4100 updateSymtabEntry(name, normalized);
4101 }
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119 private void executeNextfile(PositionTracker position) {
4120 if (nextFileAddress == null || !inputFileLoopStarted) {
4121 throw new AwkRuntimeException(position.lineNumber(), "`nextfile' cannot be called from a BEGIN rule");
4122 }
4123 if (withinEndBlocks) {
4124 throw new AwkRuntimeException(position.lineNumber(), "`nextfile' cannot be called from an END rule");
4125 }
4126 if (withinEndFileBlocks) {
4127 throw new AwkRuntimeException(position.lineNumber(), "`nextfile' cannot be called from an ENDFILE rule");
4128 }
4129
4130 resetCallState();
4131 if (endFileAddress == null || withinBeginFileBlocks && jrt.hasPendingInputFileError(resolvedInputSource)) {
4132
4133
4134
4135 withinBeginFileBlocks = false;
4136 position.jump(nextFileAddress);
4137 } else {
4138 withinBeginFileBlocks = false;
4139 withinEndFileBlocks = true;
4140 position.jump(endFileAddress);
4141 }
4142 }
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162 private void executeNext(PositionTracker position) {
4163 if (withinEndBlocks) {
4164 throw new AwkRuntimeException(position.lineNumber(), "`next' cannot be called from a `END' rule");
4165 }
4166 if (withinBeginFileBlocks) {
4167 throw new AwkRuntimeException(position.lineNumber(), "`next' cannot be called from a `BEGINFILE' rule");
4168 }
4169 if (withinEndFileBlocks) {
4170 throw new AwkRuntimeException(position.lineNumber(), "`next' cannot be called from a `ENDFILE' rule");
4171 }
4172 if (nextAddress == null || !mainInputLoopStarted) {
4173 throw new AwkRuntimeException(position.lineNumber(), "`next' cannot be called from a `BEGIN' rule");
4174 }
4175
4176 resetCallState();
4177 position.jump(nextAddress);
4178 }
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192 private boolean isMainInputFileBounded() {
4193 return endFileAddress != null && inputFileLoopStarted && !withinEndBlocks;
4194 }
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210 private void checkGetlineAllowed(PositionTracker position) {
4211 if (withinBeginFileBlocks || withinEndFileBlocks) {
4212 throw new AwkRuntimeException(
4213 position.lineNumber(),
4214 "non-redirected `getline' invalid inside `" + (withinBeginFileBlocks ? "BEGINFILE" : "ENDFILE") + "' rule");
4215 }
4216 }
4217
4218
4219 @Override
4220 public Object getFS() {
4221 return jrt.getFSVar();
4222 }
4223
4224
4225 @Override
4226 public Object getCONVFMT() {
4227 return jrt.getCONVFMTString();
4228 }
4229
4230
4231 @Override
4232 public void resetFNR() {
4233 jrt.setFNR(0);
4234 }
4235
4236
4237 @Override
4238 public void incFNR() {
4239 long v = jrt.getFNR();
4240 jrt.setFNR(v + 1);
4241 }
4242
4243
4244 @Override
4245 public void incNR() {
4246 long v = jrt.getNR();
4247 jrt.setNR(v + 1);
4248 }
4249
4250
4251 @Override
4252 public void setNF(Integer newNf) {
4253 jrt.setNF(newNf);
4254 }
4255
4256
4257 @Override
4258 public void setFILENAME(String filename) {
4259 jrt.setFILENAMEViaJrt(jrt.toInputScalar(filename));
4260 }
4261
4262
4263 @Override
4264 public Object getARGV() {
4265 if (argvOffset == NULL_OFFSET) {
4266 Map<Object, Object> argv = newAwkArray();
4267 forEachArgvEntry(argv::put);
4268 return argv;
4269 }
4270 return runtimeStack.getVariable(argvOffset, true);
4271 }
4272
4273
4274 @Override
4275 public Object getARGC() {
4276 if (argcOffset == NULL_OFFSET) {
4277 return Long.valueOf(arguments.size() + 1);
4278 }
4279 return runtimeStack.getVariable(argcOffset, true);
4280 }
4281
4282 private String getOFMT() {
4283 return jrt.getOFMTString();
4284 }
4285
4286 private Map<Object, Object> newAwkArray() {
4287 return JRT.createAwkMap(sortedArrayKeys);
4288 }
4289
4290 private Map<Object, Object> ensureMapVariable(long offset, boolean isGlobal) {
4291 return toMap(resolveVariable(offset, isGlobal, true));
4292 }
4293
4294 private Map<Object, Object> getMapVariable(long offset, boolean isGlobal) {
4295 return toMap(resolveVariable(offset, isGlobal, true));
4296 }
4297
4298 private Object resolveVariable(long offset, boolean isGlobal, boolean arrayContext) {
4299 Object value = runtimeStack.getVariable(offset, isGlobal);
4300
4301
4302 if (!isGlobal && value instanceof ArgumentReference) {
4303 value = resolveArgumentReference((ArgumentReference) value, arrayContext);
4304 runtimeStack.setVariable(offset, value, isGlobal);
4305 return value;
4306 }
4307 if (!isUntyped(value)) {
4308 return value;
4309 }
4310 value = arrayContext ? newAwkArray() : BLANK;
4311 runtimeStack.setVariable(offset, value, isGlobal);
4312 return value;
4313 }
4314
4315 private Object resolveRawArgumentReference(ArgumentReference reference) {
4316 Object currentValue = readCurrentArgumentValue(reference);
4317 if (currentValue instanceof Map
4318 || currentValue instanceof UninitializedObject && !(currentValue instanceof UntypedObject)) {
4319 return currentValue;
4320 }
4321 Object value = reference.snapshot();
4322 return value instanceof ArgumentReference ? resolveRawArgumentReference((ArgumentReference) value) : value;
4323 }
4324
4325 private Object readCurrentArgumentValue(ArgumentReference reference) {
4326 Object value = reference.currentValue();
4327 return value instanceof ArgumentReference ? readCurrentArgumentValue((ArgumentReference) value) : value;
4328 }
4329
4330 private Object resolveArgumentReference(ArgumentReference reference, boolean arrayContext) {
4331 if (!arrayContext) {
4332 checkScalar(readCurrentArgumentValue(reference));
4333 }
4334 Object value = arrayContext ? reference.currentValue() : reference.snapshot();
4335 if (value instanceof ArgumentReference) {
4336 value = resolveArgumentReference((ArgumentReference) value, arrayContext);
4337 if (arrayContext) {
4338 reference.setValue(value);
4339 } else {
4340 reference.setScalarValue(value);
4341 }
4342 return value;
4343 }
4344 if (!isUntyped(value)) {
4345 return value;
4346 }
4347 value = arrayContext ? newAwkArray() : BLANK;
4348 if (arrayContext) {
4349 reference.setValue(value);
4350 } else {
4351 reference.setScalarValue(value);
4352 }
4353 return value;
4354 }
4355
4356 private Object resolveLengthArgumentReference(ArgumentReference reference) {
4357 Object currentValue = readCurrentArgumentValue(reference);
4358 return currentValue instanceof Map ? currentValue : resolveArgumentReference(reference, false);
4359 }
4360
4361 private Map<IndirectArrayArgumentReference, Object> captureAttachedArrayArgumentValues() {
4362 if (elementArgumentReferences.isEmpty()) {
4363 return Collections.emptyMap();
4364 }
4365 Map<IndirectArrayArgumentReference, Object> values = new IdentityHashMap<IndirectArrayArgumentReference, Object>();
4366 for (IndirectArrayArgumentReference reference : elementArgumentReferences) {
4367 if (reference.isAttached()) {
4368 values.put(reference, reference.currentValue());
4369 }
4370 }
4371 return values;
4372 }
4373
4374 private void detachReplacedArrayArgumentReferences(Map<IndirectArrayArgumentReference, Object> attachedValues) {
4375 for (Map.Entry<IndirectArrayArgumentReference, Object> entry : attachedValues.entrySet()) {
4376 entry.getKey().detachIfReplaced(entry.getValue());
4377 }
4378 }
4379
4380 private void detachMissingArrayArgumentReferences(Map<Object, Object> map) {
4381 if (elementArgumentReferences.isEmpty()) {
4382 return;
4383 }
4384 for (IndirectArrayArgumentReference reference : elementArgumentReferences) {
4385 reference.detachIfMissing(map);
4386 }
4387 }
4388
4389 private static boolean isUntyped(Object value) {
4390 return value == null || value instanceof UntypedObject;
4391 }
4392
4393
4394
4395
4396
4397
4398
4399
4400 private Map<Object, Object> toMap(Object value) {
4401 if (!(value instanceof Map)) {
4402 throw new AwkRuntimeException("Attempting to use a scalar as an array.");
4403 }
4404 @SuppressWarnings("unchecked")
4405 Map<Object, Object> map = (Map<Object, Object>) value;
4406 return map;
4407 }
4408
4409
4410
4411
4412
4413
4414
4415
4416 private void checkScalar(Object value) {
4417 if (value instanceof Map) {
4418 throw new AwkRuntimeException("Attempting to use an array in a scalar context.");
4419 }
4420 }
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432 private Map<Object, Object> ensureArrayInArray(Map<Object, Object> map, Object key) {
4433 checkScalar(key);
4434 boolean existingKey = JRT.containsAwkKey(map, key);
4435 Object value = JRT.getAssocArrayValue(map, key);
4436 if (!existingKey || value == null || value instanceof UntypedObject) {
4437 Map<Object, Object> nested = newAwkArray();
4438 map.put(key, nested);
4439 return nested;
4440 }
4441 if (!(value instanceof Map)) {
4442 throw new AwkRuntimeException("Attempting to use a scalar as an array.");
4443 }
4444 @SuppressWarnings("unchecked")
4445 Map<Object, Object> nested = (Map<Object, Object>) value;
4446 return nested;
4447 }
4448
4449 private Object normalizeExternalVariableValue(Object value) {
4450 if (value instanceof String) {
4451 return jrt.toInputScalar(value);
4452 }
4453 if (!(value instanceof Map) && !(value instanceof List)) {
4454 return value;
4455 }
4456
4457
4458 return AssocArray.normalizeValue(value, sortedArrayKeys, leaf -> jrt.toInputScalar(leaf.toString()));
4459 }
4460
4461 private static final UninitializedObject BLANK = new UninitializedObject();
4462
4463 private interface ArgumentReference {
4464
4465 Object snapshot();
4466
4467 Object currentValue();
4468
4469 void setValue(Object value);
4470
4471 void setScalarValue(Object value);
4472 }
4473
4474 private static final class IndirectArgumentReference implements ArgumentReference {
4475 private final Object[] frame;
4476 private final long offset;
4477 private final Object scalarValue;
4478
4479 private IndirectArgumentReference(Object[] frameParam, long offsetParam, Object scalarValueParam) {
4480 frame = frameParam;
4481 offset = offsetParam;
4482 scalarValue = scalarValueParam;
4483 }
4484
4485 @Override
4486 public Object snapshot() {
4487 return scalarValue;
4488 }
4489
4490 @Override
4491 public Object currentValue() {
4492 return frame[(int) offset];
4493 }
4494
4495 @Override
4496 public void setValue(Object value) {
4497 frame[(int) offset] = value;
4498 }
4499
4500 @Override
4501 public void setScalarValue(Object value) {
4502 if (isUntyped(currentValue())) {
4503 setValue(value);
4504 }
4505 }
4506 }
4507
4508 private static final class IndirectArrayArgumentReference implements ArgumentReference {
4509 private final Map<Object, Object> map;
4510 private final Object key;
4511 private Object detachedValue;
4512 private boolean detached;
4513
4514
4515 private int ownerFrame = -1;
4516
4517 private IndirectArrayArgumentReference(Map<Object, Object> mapParam, Object keyParam, Object scalarValueParam) {
4518 map = mapParam;
4519 key = keyParam;
4520 detachedValue = scalarValueParam;
4521 detached = !JRT.containsAwkKey(map, key);
4522 }
4523
4524 @Override
4525 public Object snapshot() {
4526 return detachedValue;
4527 }
4528
4529 @Override
4530 public Object currentValue() {
4531 return !detached && JRT.containsAwkKey(map, key) ? JRT.getAssocArrayValue(map, key) : detachedValue;
4532 }
4533
4534 @Override
4535 public void setValue(Object value) {
4536 if (!detached && JRT.containsAwkKey(map, key)) {
4537 map.put(key, value);
4538 } else {
4539 detachedValue = value;
4540 }
4541 }
4542
4543 @Override
4544 public void setScalarValue(Object value) {
4545 if (detached || !JRT.containsAwkKey(map, key)) {
4546 detachedValue = value;
4547 } else if (isUntyped(JRT.getAssocArrayValue(map, key))) {
4548 map.put(key, value);
4549 }
4550 }
4551
4552 private void detachIfMissing(Map<Object, Object> candidateMap) {
4553 if (!detached && map == candidateMap && !JRT.containsAwkKey(map, key)) {
4554 detached = true;
4555 }
4556 }
4557
4558 private boolean isAttached() {
4559 return !detached && JRT.containsAwkKey(map, key);
4560 }
4561
4562 private void detachIfReplaced(Object previousValue) {
4563 if (!detached && (!JRT.containsAwkKey(map, key) || JRT.getAssocArrayValue(map, key) != previousValue)) {
4564 detached = true;
4565 }
4566 }
4567 }
4568
4569
4570
4571
4572
4573 private static final Set<String> NON_PERSISTENT_GLOBALS = new HashSet<>(
4574 Arrays.asList("ARGV", "ARGC", "ENVIRON", "RSTART", "RLENGTH", "IGNORECASE", "SYMTAB", "FUNCTAB"));
4575
4576 private static final class SingleRecordInputSource implements InputSource {
4577
4578 private final String record;
4579 private boolean consumed;
4580
4581 private SingleRecordInputSource(String record) {
4582 this.record = record;
4583 }
4584
4585 @Override
4586 public boolean nextRecord() {
4587 if (consumed || record == null) {
4588 return false;
4589 }
4590 consumed = true;
4591 return true;
4592 }
4593
4594 @Override
4595 public String getRecordText() {
4596 return consumed ? record : null;
4597 }
4598
4599 @Override
4600 public List<String> getFields() {
4601 return null;
4602 }
4603
4604 @Override
4605 public boolean isFromFilenameList() {
4606 return false;
4607 }
4608 }
4609
4610
4611
4612
4613 public static final int NULL_OFFSET = -1;
4614
4615 }