Add unpack operator to bytecode interpreter#222
Merged
Conversation
The parser injects the default array (e.g. @main::ARGV at top level, @_ inside a sub) directly as OperatorNode("@", IdentifierNode) on the shift/pop node's operand. The bytecode compiler handlers were expecting the operand to always be a ListNode wrapper, causing: shift requires array argument at -e line 2, near "say shift" Fix: accept operand as either: - OperatorNode("@", ...) directly (default @_/@argv case) - ListNode containing OperatorNode("@", ...) (explicit array case) Verified: ./jperl --interpreter -E 'say shift' 123 => 123 ./jperl --interpreter -E 'say pop' 123 => 123 sub foo { say shift } foo("hello") => hello my @A=(1,2,3); say shift @A; say pop @A => 1 / 3
pack was already implemented but unpack was missing, causing:
Unsupported operator: unpack at ExifTool.pm line 2257
Changes:
- Opcodes.java: add UNPACK = 305
- CompileOperator.java: add 'unpack' to the generic list-op handler
- MiscOpcodeHandler.java: add UNPACK -> Unpack.unpack(ctx, argsArray)
- BytecodeInterpreter.java: add UNPACK to the MiscOpcodeHandler case group
- InterpretedCode.java: add disassembly for all misc list operators
(PACK, UNPACK, CRYPT, LOCALTIME, GMTIME, CHMOD, UNLINK, etc.)
Verified:
pack('CC', 65, 66) => 2-byte string
unpack('CC', $packed) => (65, 66)
Three fixes:
1. push/unshift return value was lastResultReg=-1 (crash when result used)
- ARRAY_PUSH/ARRAY_UNSHIFT now emit ARRAY_SIZE after the push so
lastResultReg holds the new array size, matching Perl semantics
- Fixes: my $n = push @A, 1 and push @A, $x unless grep ...
2. push/unshift @{expr} with arbitrary expression (e.g. @{{key}})
- Previously only handled @$scalar, not @{block_expr}
- Now evaluates any non-IdentifierNode operand of @ and derefs it
- Fixes: push @{{$baseType}}, $type (ExifTool.pm)
3. Removed temporary debug code (JPERL_DEBUG printStackTrace)
Add SET_PACKAGE and PUSH_PACKAGE opcodes emitted by the bytecode compiler
when package declarations execute. The interpreter handles these by updating
InterpreterState.currentPackage (a ThreadLocal RuntimeScalar), also managed
via DynamicVariableManager for scoped package blocks (package Foo { }).
This fixes:
- caller() returning wrong package in interpreter mode (Exporter::export_ok_tags)
- eval STRING compiling in wrong package in interpreter mode
- ExceptionFormatter now uses runtime package for innermost interpreter frame
- ExceptionFormatter consumes InterpreterState frames in order instead of
always using current() which always returned the same topmost frame
…ad/write/open, socket, bind, connect, listen, write, formline, printf, accept, sysseek, truncate, read
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
unpackwas missing from the bytecode interpreter, causing:packwas already implemented butunpackwas not.Changes
Opcodes.java: addUNPACK = 305CompileOperator.java: addunpackto the generic list-op handler (same path aspack)MiscOpcodeHandler.java: addUNPACK -> Unpack.unpack(ctx, argsArray)— one line, reuses existing infrastructureBytecodeInterpreter.java: addUNPACKto theMiscOpcodeHandlercase groupInterpretedCode.java: add disassembly for all misc list operators (PACK, UNPACK, CRYPT, LOCALTIME, GMTIME, CHMOD, UNLINK, etc.) — previously they fell through to UNKNOWNVerified