Граф коммитов

596 Коммитов

Автор SHA1 Сообщение Дата
Kevin Newton 98823b5091 [PRISM] Remove dummy_line_node usage from local variable nodes 2024-04-04 14:14:55 -04:00
Kevin Newton 0782fc4a2a [PRISM] Remove dummy_line_node usage from match predicate node 2024-04-04 14:14:55 -04:00
Kevin Newton 95ad2b0a62 [PRISM] Remove dummy_line_node usage from match required node 2024-04-04 14:14:55 -04:00
Kevin Newton c9f80021e2 [PRISM] Simplify match write node compilation 2024-04-04 14:14:55 -04:00
Kevin Newton 69ec4990d8 [PRISM] Remove dummy_line_node usage from module and match write node 2024-04-04 14:14:55 -04:00
Kevin Newton 449384f337 [PRISM] Remove dummy_line_node usage for multi write node 2024-04-04 14:14:55 -04:00
Kevin Newton 5e8546e8a9 [PRISM] Remove dummy_line_node usage for next node 2024-04-04 14:14:55 -04:00
Kevin Newton 24f9faf67c [PRISM] Remove dummy_line_node usage for call operator write node 2024-04-04 14:14:55 -04:00
Kevin Newton 4d347a0a55 [PRISM] Remove dummy_line_node usage for call and/or write node 2024-04-04 14:14:55 -04:00
Kevin Newton 91373bd64d [PRISM] Remove dummy_line_node usage for call nodes 2024-04-04 14:14:55 -04:00
Kevin Newton eb3bad1a0f [PRISM] Remove dummy_line_node usage for break nodes 2024-04-04 14:14:55 -04:00
Kevin Newton ffd9402c8d [PRISM] Remove dummy_line_node usage for block argument nodes 2024-04-04 14:14:55 -04:00
Kevin Newton c15c611ea5 [PRISM] Remove dummy_line_node usage for begin nodes 2024-04-04 14:14:55 -04:00
Kevin Newton 59f931e4f7 [PRISM] Remove dummy_line_node usage for PM_BACK_REFERENCE_READ_NODE 2024-04-04 14:14:55 -04:00
Kevin Newton 6c1cf5232f [PRISM] Remove dummy_line_node usage in pm_interpolated_node_compile 2024-04-04 14:14:55 -04:00
Kevin Newton ced6aff206 [PRISM] Mark constant path owning expressions as not popped 2024-04-03 17:34:12 -04:00
Kevin Newton 8191735b73 [PRISM] Fix BEGIN{} execution order 2024-03-29 08:51:04 -04:00
Kevin Newton a8f902ea8e [PRISM] Add debug info for frozen strings 2024-03-28 15:17:29 -04:00
Kevin Newton 86e0d83a32 [PRISM] Simplify raising load errors 2024-03-28 12:04:35 -04:00
Kevin Newton f7c5e11d89 [PRISM] Use new -x prism API 2024-03-28 12:04:35 -04:00
Kevin Newton 7e12b03c5a [PRISM] Set path on syntax error 2024-03-27 17:19:24 -04:00
Kevin Newton 39606f36e3 [PRISM] Implicitly change encoding when a UTF-8 BOM is found 2024-03-27 14:19:58 -04:00
Kevin Newton ab2ee308aa [PRISM] Match style for invalid encoding error 2024-03-27 14:19:58 -04:00
Kevin Newton eb995a6410 [PRISM] Include file and line in error message 2024-03-27 14:19:58 -04:00
Kevin Newton a69f0047cb [PRISM] Use new error formatting API 2024-03-27 13:03:11 -04:00
Kevin Newton 42d1cd8f7f [PRISM] Pass --enable-frozen-string-literal through to evals 2024-03-27 08:34:42 -04:00
Kevin Newton e4b2109065 [PRISM] Fix ASCII-compatible check for eval encoding 2024-03-27 08:34:42 -04:00
Kevin Newton 0c62eb25b5 [PRISM] Use correct encoding for regular expression literals 2024-03-26 15:32:09 -04:00
Kevin Newton 4300c42a7e [PRISM] Better handle interpolated* nodes with inner frozen parts 2024-03-26 12:11:09 -04:00
Étienne Barrié 12be40ae6b Implement chilled strings
[Feature #20205]

As a path toward enabling frozen string literals by default in the future,
this commit introduce "chilled strings". From a user perspective chilled
strings pretend to be frozen, but on the first attempt to mutate them,
they lose their frozen status and emit a warning rather than to raise a
`FrozenError`.

Implementation wise, `rb_compile_option_struct.frozen_string_literal` is
no longer a boolean but a tri-state of `enabled/disabled/unset`.

When code is compiled with frozen string literals neither explictly enabled
or disabled, string literals are compiled with a new `putchilledstring`
instruction. This instruction is identical to `putstring` except it marks
the String with the `STR_CHILLED (FL_USER3)` and `FL_FREEZE` flags.

Chilled strings have the `FL_FREEZE` flag as to minimize the need to check
for chilled strings across the codebase, and to improve compatibility with
C extensions.

Notes:
  - `String#freeze`: clears the chilled flag.
  - `String#-@`: acts as if the string was mutable.
  - `String#+@`: acts as if the string was mutable.
  - `String#clone`: copies the chilled flag.

Co-authored-by: Jean Boussier <byroot@ruby-lang.org>
2024-03-19 09:26:49 +01:00
Kevin Newton b7ca4b1e2d [PRISM] Fix up frozen checks for aref 2024-03-18 11:55:43 -04:00
Jean Boussier 91bf7eb274 Refactor frozen_string_literal check during compilation
In preparation for https://bugs.ruby-lang.org/issues/20205.

The `frozen_string_literal` compilation option will no longer
be a boolean but a tri-state: `on/off/default`.
2024-03-15 15:52:33 +01:00
Kevin Newton b4f3f3c103
[PRISM] Fix up source file when not frozen 2024-03-15 09:47:49 -04:00
Kevin Newton 043c90e161
Fix compiling shareable constant nodes 2024-03-15 09:42:44 -04:00
Kevin Newton 1e7c0fcc9a [PRISM] Fix float in case dispatch 2024-03-14 21:59:26 -04:00
Jean Boussier 09d8c99cdc Ensure test suite is compatible with --frozen-string-literal
As preparation for https://bugs.ruby-lang.org/issues/20205
making sure the test suite is compatible with frozen string
literals is making things easier.
2024-03-14 17:56:15 +01:00
Kevin Newton 2f8cbd6431 [PRISM] Handle ambiguous_param0 for it and numbered parameters 2024-03-13 19:01:33 -04:00
Kevin Newton 7c9e6eab89 Handle nth ref too big 2024-03-13 19:01:21 -04:00
Kevin Newton fa009e392c [PRISM] Static literal strings should be fstrings 2024-03-13 14:24:53 -04:00
Kevin Newton e6a1baad48 [PRISM] Fix compiling duplicated keywords 2024-03-13 14:13:19 -04:00
Kevin Newton 22217e70c2
[PRISM] Handle parentheses in patterns in prism 2024-03-13 10:06:19 -04:00
Kevin Newton a6dac9bb4f [PRISM] Parse stdin on CLI with prism 2024-03-11 12:51:32 -04:00
Kevin Newton 811296b4af [PRISM] Use new integer logic 2024-03-07 18:02:33 -05:00
cui fliter 226a889dc7
[DOC] fix some comments
Signed-off-by: cui fliter <imcusg@gmail.com>
2024-03-05 18:50:47 +09:00
Kevin Newton a29c0e827f [PRISM] Handle implicit flip-flop bounds checking against $. 2024-02-28 13:26:00 -05:00
Kevin Newton 16c7144cd5 [PRISM] Only look up encoding once per file 2024-02-28 12:44:24 -05:00
Kevin Newton f8355e88d6 [PRISM] Do not load -r until we check if main script can be read 2024-02-28 12:42:57 -05:00
Kevin Newton 4016535404 [PRISM] Integrate new number parsing 2024-02-22 22:42:44 -05:00
Kevin Newton be96dc9f8c [PRISM] Support it local variable 2024-02-21 17:55:11 -05:00
Kevin Newton 5c02d97780 [PRISM] Remove more dummy line usage
* or
* optional parameter
* parentheses
* pre execution
* post execution
* range
* rational
* redo
2024-02-21 14:14:21 -05:00
Kevin Newton e19fde52cc [PRISM] Remove more dummy line usage
* if
* unless
2024-02-21 14:14:21 -05:00
Kevin Newton e1f6b477e0 [PRISM] Remove more dummy line usage
* regular expressions
* rescue
* rescue modifier
2024-02-21 14:14:21 -05:00
Kevin Newton e7d480df5d [PRISM] Remove more dummy line usage
* returns
* retry
* self
* singleton class
2024-02-21 14:14:21 -05:00
Kevin Newton 61c5936da3 [PRISM] Remove more dummy line usage
* source files
* source encodings
* source lines
2024-02-21 14:14:21 -05:00
Kevin Newton 220ced7486 [PRISM] Remove more dummy line usage
* yields
* xstrings
* while
* until
* undef
* true
* symbol
* super
* string
* statements
* splat
2024-02-21 14:14:21 -05:00
Peter Zhu 777c4da0bf [PRISM] GC guard strings
Using RSTRING_PTR can cause the string object to not exist on the stack,
which could cause it to be GC'd or be moved by GC compaction. This can
cause RSTRING_PTR to point to the incorrect location if the string is
embedded and moved by GC compaction.

Fixes ruby/prism#2442.
2024-02-16 15:02:03 -05:00
Kevin Newton 9933377c34 [PRISM] Correctly hook up line numbers for eval 2024-02-14 15:29:26 -05:00
Kevin Newton fc2c128e7e [PRISM] Set eval encoding based on string encoding 2024-02-14 13:24:43 -05:00
Matt Valentine-House f70a4a0336 [PRISM] yield is invalid inside eval 2024-02-13 21:19:12 -05:00
Matt Valentine-House 0285b533e9 [PRISM] Don't rb_bug if redo/break/next used during eval 2024-02-13 21:19:12 -05:00
Peter Zhu a5869e5d8c [PRISM] Replace assert with RUBY_ASSERT
assert does not print the bug report, only the file and line number of
the assertion that failed. RUBY_ASSERT prints the full bug report, which
makes it much easier to debug.
2024-02-13 15:54:04 -05:00
Peter Zhu 83c158fdfc [PRISM] Replace assert with rb_bug 2024-02-13 15:54:04 -05:00
Kevin Newton 4e481c772e [PRISM] Refactors to use more locations 2024-02-13 10:26:42 -05:00
Kevin Newton fe31b682be [PRISM] Stop passing parser around so much 2024-02-13 10:26:42 -05:00
Kevin Newton e967e06276 [PRISM] Switch to locations for PM_ALIAS_GLOBAL_VARIABLE_NODE, PM_ALIAS_METHOD_NODE, and PM_AND_NODE 2024-02-13 10:26:42 -05:00
Kevin Newton 76ab017f01 [PRISM] Brace style in prism_compile.c 2024-02-13 10:26:42 -05:00
Nikita Vasilevsky d357d50f0a [PRISM] Fix lambda start column number
Co-Authored-By: Kevin Newton <kddnewton@gmail.com>
2024-02-13 09:39:28 -05:00
Kevin Newton b662edf0a0 [PRISM] Combine hash compilation between hashes and keywords 2024-02-13 09:37:24 -05:00
Kevin Newton c0e121420b [PRISM] Fix compilation of hash with multiple merges 2024-02-13 09:37:24 -05:00
Kevin Newton 1395838e18 [PRISM] Check full lines for invalid UTF-8 2024-02-12 14:48:09 -05:00
eileencodes a3ceb69168 [PRISM] Fix error handling in `pm_parse_prism`
Following changes made in ruby/prism#2365 this implements error handling
for when `pm_string_mapped_init` returns `false`.

Related: ruby/prism#2207
2024-02-11 09:41:20 -05:00
Kevin Newton f7467e70e1 Split line_no and node_id before new_insn_body
Before this commit, there were many places where we had to generate
dummy line nodes to hold both the line number and the node id that
would then immediately get pulled out from the created node. Now
we pass them explicitly so that we don't have to generate these
nodes.

This makes a clearer line between the parser and compiler, and also
makes it easier to generate instructions when we don't have a
specific node to tie them to. As such, it removes almost every
single place where we needed to previously generate dummy nodes.

This also makes it easier for the prism compiler, because now we
can pass in line number and node id instead of trying to generate
dummy nodes for every instruction that we compile.
2024-02-09 17:01:27 -05:00
Kevin Newton e96c838ca4 [PRISM] Fix flaky memory in scope nodes 2024-02-09 16:30:07 -05:00
Kevin Newton 5c2d96df19 [PRISM] Implement opt_case_dispatch 2024-02-09 11:26:32 -05:00
Kevin Newton a4ba62b6e5 [PRISM] Refactor case nodes for only one pass through when 2024-02-09 11:26:32 -05:00
Kevin Newton 3e03981f25 [PRISM] Compile constant paths with optimizations 2024-02-08 11:53:10 -05:00
Kevin Newton 54295ba5e1 [PRISM] Compile constant reads using opt_getconstant_path 2024-02-08 11:53:10 -05:00
Kevin Newton b2d468fced [PRISM] Refactor call opts to only check for specific ids 2024-02-08 11:53:10 -05:00
Nikita Vasilevsky c3403322df [PRISM] Use block node location when building block iseq
Co-Authored-By: Kevin Newton <kddnewton@gmail.com>
2024-02-06 14:56:10 -05:00
Kevin Newton c1bc714720 [PRISM] Do not show source snippets if non-UTF-8 2024-02-06 14:31:10 -05:00
Kevin Newton ccec209b2c [PRISM] Fix fsl coming from file 2024-02-06 12:36:46 -05:00
Kevin Newton d77172b79b [PRISM] Fix deconstruct index for posts 2024-02-06 11:57:59 -05:00
Kevin Newton 26fac8f6fa [PRISM] Set correct call target flags 2024-02-06 11:21:52 -05:00
Kevin Newton c6d4071c29 [PRISM] Handle match write popped 2024-02-06 11:08:14 -05:00
Kevin Newton 1c120023f1 [PRISM] Correctly handle popped attr write 2024-02-06 10:46:27 -05:00
Kevin Newton b801a19c6a [PRISM] Handle empty interpolated statements at start of string 2024-02-06 10:13:11 -05:00
Kevin Newton d6b7eae58e [PRISM] Correct spec for defined? parentheses 2024-02-06 09:51:25 -05:00
Kevin Newton c5694c647a [PRISM] Raise ArgumentError for invalid encoding 2024-02-05 15:18:15 -05:00
Kevin Newton 10a182f597 [PRISM] Fix method return line 2024-02-05 14:42:20 -05:00
Kevin Newton 7eea066cb4 [PRISM] Fix pattern matching array with implicit rest 2024-02-05 12:54:52 -05:00
Kevin Newton ae7816bc20
[PRISM] Fix up idUMinus optimization 2024-02-05 11:57:38 -05:00
Peter Zhu 948c618bda [PRISM] Fix encoding of interpolated strings
Fixes ruby/prism#2313.
2024-02-05 11:55:44 -05:00
Jenny Shen b35cdb4758 [PRISM] Implement opt_aset_with
Part of ruby/prism#2231

Co-authored-by: Adrianna Chang <adrianna.chang@shopify.com>
Co-authored-by: Peter Zhu <peter@peterzhu.ca>
2024-02-05 10:27:58 -05:00
Jenny Shen 8ed26a3f59 [PRISM] Refactor PM_CALL_NODE conditional
Co-authored-by: Peter Zhu <peter@peterzhu.ca>
2024-02-05 10:27:58 -05:00
Jenny Shen 8bc6fff322 [PRISM] Do not optimize safe navigation aref
Co-authored-by: Peter Zhu <peter@peterzhu.ca>
2024-02-04 11:05:02 -05:00
Jenny Shen c5d4444761 [PRISM] Do not optimize safe navigation -@/freeze
Co-authored-by: Peter Zhu <peter@peterzhu.ca>
2024-02-04 11:05:02 -05:00
Alan Wu 0854d64862 [PRISM] Selectively dup array in `foo(*splat, &block_arg)` calls
This is essentially an adaptation of aae8223c70 ("Dup splat array in
certain cases where there is a block argument") to pass
`TestCall#test_call_block_order`.

This also makes PRISM emit `getblockparamproxy` in cases like
`def foo(&blk) = bar(&blk)` where it used the less efficient
`getblockparam` previously.

Remove the `popped` parameter from setup_args() because it should always
be ignored, and is in fact unused.
2024-02-02 22:25:08 -05:00
Alan Wu 90ae8eaeca [PRISM] Fix numbered parameters stealing local names
Previously, the local index of numbered parameters were assigned to
names of regular locals, making it hard to read both of them. Use proper
`_[1-9]` numbered parameters. This fixes `test_shapes.rb`.

Also, properly mark the iseq as having lead parameters.
2024-02-02 18:50:41 -05:00
Peter Zhu 24cdafb2f8 [PRISM] Fix indentation of pm_scope_node_init [ci skip] 2024-02-02 14:36:08 -05:00
Alan Wu 8d33be9833
[PRISM] Respect string encoding override in array literals
Fixes `TestZlibGzipReader#test_gets2`,
`Psych_Unit_Tests#test_spec_explicit_families`, and many failures in
`test_unicode_normalize.rb`.
2024-02-01 22:11:32 +00:00
Jenny Shen 4fbdbde088 [PRISM] dedup hash string keys
Fixes ruby/prism#2321

Co-authored-by:  Adrianna Chang <adrianna.chang@shopify.com>
Co-authored-by:  Peter Zhu <peter@peterzhu.ca>
2024-02-01 15:52:33 -05:00
Kevin Newton 332d2c92d8 [PRISM] Emit parse warnings 2024-02-01 15:52:19 -05:00
Alan Wu b47d43fa9b [PRISM] Use rb_fstring() on all string literals
In addition to saving space by deduplicating, this also makes the
literals have code range like TestObjSpace#test_dump_string_coderange
expects. It's testing a detail, but we might as well use rb_fstring().

Note that `putstring` makes a mutable duplicate, so passing it an
fstring is fine.
2024-02-01 15:50:32 -05:00
Nikita Vasilevsky c7fe3ecb49 [prism] Use block opening line as `source_location` line of lambda
There are several prism tests failing related to the `source_location`
for lambda returning line of the operator (`->`)
while original parser execution results in `source_location` line
pointing to the block opening location (`{` or `do`)

This commit changes `PM_LAMBDA_NODE` compilation case
to use block opening location instead of the whole node (operator)
opening location to get the line number to build block iseq
2024-02-01 15:23:39 -05:00
Alan Wu 1f226b41f0 [PRISM] Fix multiple return with splat and splat+kwsplat
Previously, `return *array, 1` didn't behave like `return [*array, 1]`
properly. Also, it crashed when splat and kwsplat is combined like in
`array = [*things, **hash]`.

Fix this by grouping `PM_ARGUMENTS_NODE` with `PM_ARRAY_NODE` handling and
combining splat and kwsplat handling.
2024-02-01 10:33:54 -05:00
Kevin Newton a7b47f96f3 [PRISM] Add splatkw to super calls when necessary 2024-01-31 22:18:55 -05:00
Kevin Newton d96b4586e4 [PRISM] Fix up index write nodes to match 2024-01-31 22:18:55 -05:00
Kevin Newton d36c31ed9b [PRISM] Fix keywords in index write nodes 2024-01-31 22:18:55 -05:00
Kevin Newton 8acd1f708f [PRISM] Fix combination of safe navigation and ||= and &&= operators 2024-01-31 22:18:55 -05:00
Kevin Newton 4d01c59042 [PRISM] Fix combination of attribute write and safe navigation 2024-01-31 22:18:55 -05:00
Kevin Newton b5a2c60d0a [PRISM] Support SCRIPT_LINES__ 2024-01-31 15:49:32 -05:00
Peter Zhu cb98b018c0 [PRISM] Fix else with rescue
Fixes ruby/prism#2307.
2024-01-31 14:11:14 -05:00
Étienne Barrié c28ee91263 [PRISM] Fix incorrect use of VM_CALL_KW_SPLAT_MUT in zsuper with keyword splat
This copies the changes from 771a2f039b.
Fixes ruby/prism#2310.
2024-01-31 13:41:49 -05:00
Kevin Newton 610636fd6b [PRISM] Mirror iseq APIs
Before this commit, we were mixing a lot of concerns with the prism
compile between RubyVM::InstructionSequence and the general entry
points to the prism parser/compiler.

This commit makes all of the various prism-related APIs mirror
their corresponding APIs in the existing parser/compiler. This means
we now have the correct frame naming, and it's much easier to follow
where the logic actually flows. Furthermore this consolidates a lot
of the prism initialization, making it easier to see where we could
potentially be raising errors.
2024-01-31 13:41:36 -05:00
Matt Valentine-House 8041b7d967 [PRISM] pm_compile_logical: Fix OrNode in IfNode predicate
Fixes: https://github.com/ruby/prism/issues/2294
2024-01-30 22:22:39 +00:00
Matt Valentine-House 1142ed2f50 [PRISM] Remove unneccessary uses of cond_seq
As this is compiled into only on the recursive call and then added, we
can just use `ret` directly
2024-01-30 22:22:39 +00:00
Aaron Patterson 8e708e4a07 Update forwarding locals for prism 2024-01-30 13:19:06 -05:00
Peter Zhu c85e28d12a [PRISM] Remove unused variable 2024-01-30 10:29:11 -05:00
Kevin Newton 83966a57fe [PRISM] Method location for calls 2024-01-29 22:22:30 -05:00
Peter Zhu bbb7ab906e [PRISM] Fix crash when multiple underscores
Fixes ruby/prism#2295.
2024-01-29 17:15:33 -05:00
Kevin Newton 3fb741069d [PRISM] Fix rescue frame label 2024-01-29 16:38:11 -05:00
Peter Zhu 8e1fe15b4a [PRISM] Implement opt_str_uminus 2024-01-29 16:34:25 -05:00
Peter Zhu 32bbf47500 [PRISM] Use opt_str_freeze instruction
Fixes ruby/prism#2290.
2024-01-29 14:53:34 -05:00
Matt Valentine-House d7501c4031 [PRISM] Fix InterpolatedStringNode
If the first element of an interpolated string node is an embedded
statement, CRuby "pre-initializes" the interpolation with a string of
known encoding to concat into.

This patch replicates thate behaviour in Prism
2024-01-29 19:13:53 +00:00
Peter Zhu 3d3d9e8397 [PRISM] Support US-ASCII symbols 2024-01-29 13:56:54 -05:00
Peter Zhu d980c89273 [PRISM] Support ASCII-8BIT symbols 2024-01-29 13:56:54 -05:00
Peter Zhu f634c7a268 [PRISM] Support UTF-8 symbols
Fixes ruby/prism#2242.
2024-01-29 13:56:54 -05:00
Peter Zhu 9a5a11f3d0 [PRISM] Use the splatkw instruction
Fixes ruby/prism#2272.
2024-01-29 11:42:40 -05:00
Matt Valentine-House c422805bbb [PRISM] Fix encoding for interpolated strings.
This wasn't taking into account strings with flags set on the containing
node that could affect the encoding. It does now
2024-01-29 10:04:16 -05:00
Kevin Newton cb9a47f2ac [PRISM] Fix branchif ADD_INSN1 2024-01-26 16:46:31 -05:00
Peter Zhu a120529020 [PRISM] Fix loop in rescue blocks
Fixes ruby/prism#2250.

Co-Authored-By: Kevin Newton <kddnewton@gmail.com>
2024-01-26 16:07:24 -05:00
Peter Zhu 59bb78ebd0 [PRISM] Fix for s390x
On s390x, a long is 8 bytes. st_data_t is an unsigned long but
pm_constant_id_t is a 4 byte integer. We need to cast it to st_data_t
when passing it to ST functions.
2024-01-26 15:41:06 -05:00
Peter Zhu 3d996e827f [PRISM] Keyword arguments incorrectly passed as mutable
Fixes ruby/prism#2279.
2024-01-26 14:58:29 -05:00
Matt Valentine-House e452caac10 [PRISM] Fix next inside rescue 2024-01-25 15:10:52 -05:00
Kevin Newton 1301422dfe [PRISM] Fix VM_CALL_ARGS_SPLAT_MUT failures 2024-01-25 11:11:50 -05:00
Peter Zhu f769d68a69 [PRISM] Fix getblockparamproxy for forwarding arguments 2024-01-24 17:32:43 -05:00
Peter Zhu 7d61454f2e [PRISM] Fix method calls in keyword arguments
Fixes ruby/prism#2248.
2024-01-24 15:18:06 -05:00
Aaron Patterson 94f3f9502c Fix repeated block param
Co-Authored-By: Matt Valentine-House <matt@eightbitraptor.com>
2024-01-24 12:04:44 -08:00
Aaron Patterson 1817af5074 Fix local table size / index for repeated kwrest
Co-Authored-By: Matt Valentine-House <matt@eightbitraptor.com>
2024-01-24 12:04:44 -08:00
Aaron Patterson 13c76e40d3 Fix local table size and variable offset for repeated keywords
Co-Authored-By: Matt Valentine-House <matt@eightbitraptor.com>
2024-01-24 12:04:44 -08:00
Aaron Patterson 8b7e78f156 Fix local table size and index for required post underscore
Co-Authored-By: Matt Valentine-House <matt@eightbitraptor.com>
2024-01-24 12:04:44 -08:00
Aaron Patterson bb6af9287b Fix local table space for `*_`
We need to make sure there is enough room in the local table for
repeated `*_` parameters

Co-Authored-By: Matt Valentine-House <matt@eightbitraptor.com>
2024-01-24 12:04:44 -08:00
Aaron Patterson 03f76f098a Fix repeated optional _ parameters
Ensure there is enough space in the local table for repeated optional
parameters.

Co-Authored-By: Matt Valentine-House <matt@eightbitraptor.com>
2024-01-24 12:04:44 -08:00
Aaron Patterson 29c3ec3d49 Fix required positional repeated _ parameters
Co-Authored-By: Matt Valentine-House <matt@eightbitraptor.com>
2024-01-24 12:04:44 -08:00
Kevin Newton 44c337a397 [PRISM] Support __LINE__, __FILE__, and __ENCODING__ in patterns, fix pinned expressions 2024-01-24 15:02:32 -05:00
Kevin Newton c888b8d63b [PRISM] Handle implicit lvar write in hash pattern 2024-01-24 14:18:33 -05:00
Peter Zhu 020fe6e0ca [PRISM] Fix forwarding from within block
Fixes ruby/prism#2253.
2024-01-24 14:18:25 -05:00
Peter Zhu dc8fb7d97f [PRISM] Fix crash in anonymous block with forwarding arguments
Fixes ruby/prism#2262.
2024-01-24 13:42:21 -05:00
Aaron Patterson 578ff32611 Move filling in the rest of the locals to the end 2024-01-24 09:40:30 -08:00
Aaron Patterson 7a33a1aee2 Insert all locals in the locals index table
Prism provides an index (local_body_index) which is supposed to point at
the start of locals declared in the method body. Prism assumed that
method body locals would only occur _after_ parameter names.
Unfortunately this assumption is not correct, which meant that we would
in some cases not insert all locals in the local table.  This commit
iterates over locals a second time, inserting any that didn't get
inserted on the first pass.

Fixes: https://github.com/ruby/prism/issues/2245

Co-Authored-By: Matt Valentine-House <matt@eightbitraptor.com>
2024-01-24 09:40:30 -08:00
Peter Zhu 927928badb [PRISM] Fix anonymous keyword arguments
Fixes ruby/prism#2256.
2024-01-24 11:41:08 -05:00
Kevin Newton 4220bdba39 [PRISM] Remove dead code from prism_compile.c
Previously, we were using the main pm_compile_node switch to compile
target nodes. Eventually we switched to pm_compile_target_node and
updated all the call sites. As such, all of this has become dead
code.

However, the multi target code was reused for both parameters and
multi writes (it was just incorrect for multi writes). So that code
has now been moved into its own function that is more specific to
destructured parameters (and has more assertions to that effect).

Overall now you should not see target nodes when you call into
pm_compile_node, since they are more specialized and require more
special handling.
2024-01-24 11:18:58 -05:00
Kevin Newton 767f5d045f [PRISM] Use lvar depths for starting 2024-01-24 10:37:08 -05:00
Peter Zhu 529700d314 [PRISM] Nested MultiWriteNode with method calls
Fixes ruby/prism#2247.
2024-01-24 10:08:25 -05:00
Peter Zhu 909a710a69 [PRISM] Fix anonymous splat nodes
Fixes ruby/prism#2257.
2024-01-23 17:45:51 -05:00
Peter Zhu cecf42601a [PRISM] Fix block fowarding 2024-01-23 15:23:13 -05:00
Peter Zhu 333f4b4930 [PRISM] Support block parameters with no name
Fixes ruby/prism#2249.
2024-01-23 15:23:13 -05:00
Aaron Patterson cfa15bb173 Handle trailing commas on blocks
We need to set a special flag on block iseqs when there is a trailing
comma.

Fixes: https://github.com/ruby/prism/issues/2244
2024-01-22 16:35:58 -08:00
Aaron Patterson 270a46e392 Check keyword parameters correctly
We weren't checking the right offsets when compiling methods with
keyword parameters that had complex code.

Fixes: https://github.com/ruby/prism/issues/2228
2024-01-22 16:02:41 -08:00
Peter Zhu 708fa77404 [PRISM] Fix keyword arguments in IndexOrWriteNode
Fixes ruby/prism#2236.
2024-01-22 15:07:52 -08:00
Peter Zhu 1838dbf6e7 [PRISM] Fix splat and block in aset 2024-01-22 15:07:39 -08:00
Peter Zhu 1847192366 [PRISM] Fix block in aset
Fixes ruby/prism#2223.
2024-01-22 15:07:39 -08:00
Peter Zhu 8065672d99 [PRISM] Force semicolon at the end of PM macros 2024-01-22 15:22:12 -05:00
Peter Zhu 2630941696 [PRISM] Use PM_SWAP macro 2024-01-22 15:21:09 -05:00
Peter Zhu 789de5972b [PRISM] Use PM_POP macro 2024-01-22 15:16:14 -05:00
Aaron Patterson 7db6832225 Fix compiling rescue + ensure
When we're compiling begin / rescue / ensure nodes, we need to "wrap"
the code in the begin statements correctly.  The wrapping is like this:
(ensure code (rescue code (begin code)))

This patch pulls the each leg in to its own function, then calls the
appropriate wrapping function depending on whether there are ensure /
rescue legs.

Fixes: https://github.com/ruby/prism/issues/2221
2024-01-22 12:02:03 -08:00
Peter Zhu dbd76d9101 [PRISM] Fix keyword splat in IndexAndWriteNode and IndexOrWriteNode
Fixes ruby/prism#2232 and ruby/prism#2234.
2024-01-22 12:56:43 -05:00
Kevin Newton ab99ce910c [PRISM] Insert concatarray for index targets with splat 2024-01-22 12:55:06 -05:00
Kevin Newton e5f8585a29 [PRISM] Fix line for leave instructions 2024-01-22 12:54:32 -05:00
Peter Zhu a7af34fa8b [PRISM] Fix keywords arguments in IndexAndWriteNode
Fixes ruby/prism#2233.
2024-01-22 11:26:21 -05:00
Kevin Newton 6401f282d2
[PRISM] Fix up source line for 1-indexed line numbers 2024-01-22 11:21:04 -05:00
Kevin Newton d68aaa6672 [PRISM] Freeze regex literals in iseqs 2024-01-22 11:12:38 -05:00
Peter Zhu 580429d27c [PRISM] Fix incorrect ordering of MultiTargetNode
Fixes ruby/prism#2218.
2024-01-22 10:51:43 -05:00
Matt Valentine-House 2abf153016 [PRISM] Add TP call/return events to method ISEQs 2024-01-22 09:40:52 -05:00
Kevin Newton 99d6e2f1ee [PRISM] Revisit target nodes 2024-01-19 20:12:07 -05:00
eileencodes ed50161bd6 [PRISM] Fix ensure code running twice
Fixes: ruby/prism#2212
2024-01-19 13:04:01 -08:00
Aaron Patterson 4778b0eeda Fix kwarg ordering
Required keyword arguments need to come first.

Fixes: https://github.com/ruby/prism/issues/2158

Co-authored-by: Kevin Newton <kddnewton@gmail.com>
2024-01-19 12:32:20 -08:00
Peter Zhu 740f0b52e0 [PRISM] Fix typo with pm_scope_node_destroy
We need to run the pm_scope_node_destroy after compiling the iseq.
2024-01-19 14:09:32 -05:00
Aaron Patterson efe4b8ac0f Fix ensure code when running break in a while loop
We need to run ensure code when breaking from a while loop

Co-authored-by: John Hawthorn <jhawthorn@github.com>
Co-authored-by: Kevin Newton <kddnewton@gmail.com>
2024-01-19 09:35:36 -08:00
Peter Zhu 5a5cf23d02 [PRISM] Fix indentation for PM_SCOPE_NODE [ci skip] 2024-01-19 11:18:47 -05:00
Peter Zhu c28094d385 [PRISM] Add function to free scope node
pm_scope_node_destroy frees the scope node after we're done using it to
make sure that the index_lookup_table is not leaked.

For example:

    10.times do
      100_000.times do
        RubyVM::InstructionSequence.compile_prism("begin; 1; rescue; 2; end")
      end

      puts `ps -o rss= -p #{$$}`
    end

Before:

    33056
    50304
    67776
    84544
    101520
    118448
    135712
    152352
    169136
    186656

After:

    15264
    15296
    15408
    17040
    17152
    17152
    18320
    18352
    18400
    18608
2024-01-18 16:33:25 -05:00
Peter Zhu 47081c3ee3 [PRISM] Pass pm_scope_node_t by reference
We can pass pm_scope_node_t by reference to pm_new_child_iseq rather
than by value.
2024-01-18 16:33:25 -05:00
Matt Valentine-House 60dd731125 [PRISM] Correct checkmatch flags for splat in rescue 2024-01-18 20:25:28 +00:00
Matt Valentine-House 8a3e7f08b8 [PRISM] Fix case splat with no predicate 2024-01-18 20:03:27 +00:00
Peter Zhu 686b1655a0 [PRISM] Fix indentation in switch [ci skip] 2024-01-18 14:59:59 -05:00
Peter Zhu d8ac96efc5 [PRISM] Fix memory leak in case nodes
The temporary array conditions_labels is heap allocated and never freed.
We can use alloca instead to stack allocate it so that we don't need to
manually free it.

For example:

    code = "case; #{100.times.map { "when #{it}; " }.join}; end"

    10.times do
      10_000.times do
        RubyVM::InstructionSequence.compile_prism(code)
      end

      puts `ps -o rss= -p #{$$}`
    end

Before:

    21376
    30304
    38800
    47184
    55456
    64192
    72288
    80400
    89040
    97104

After:

    13088
    13632
    13760
    14016
    14688
    14992
    15120
    15232
    15744
    15744
2024-01-18 13:40:14 -05:00
Peter Zhu 00814fd672 [PRISM] Fix memory leak in iseq
rb_iseq_compile_prism_node calls both rb_translate_prism and iseq_setup.
Both of these functions call iseq_set_sequence. This means that the first
iseq_set_sequence will leak because the iseq will be overwritten.

For example:

    10.times do
      100_000.times do
        RubyVM::InstructionSequence.compile_prism("")
      end

      puts `ps -o rss= -p #{$$}`
    end

Before:

    20528
    27328
    33840
    40208
    46400
    52960
    59168
    65600
    71888
    78352

After:

    13696
    13712
    13712
    13712
    13712
    14352
    14352
    14992
    14992
    14992
2024-01-18 10:35:46 -05:00
eileencodes db9f5fc916 [Prism] Implement defined? for PM_UNLESS_NODE
Ruby code:

```ruby
defined?(unless true; 1; end)
```

Instructions:

```
"********* Ruby *************"
== disasm: #<ISeq:<compiled>@<compiled>:1 (1,0)-(59,16)>
0000 putobject                              "expression"              (  59)[Li]
0002 leave
"********* PRISM *************"
== disasm: #<ISeq:<compiled>@<compiled>:58 (58,0)-(58,16)>
0000 putobject                              "expression"              (  58)[Li]
0002 leave
```

Related: ruby/prism#2188
2024-01-17 17:21:29 -05:00
eileencodes 555af5e599 [Prism] Implement defined? for PM_UNTIL_NODE
Ruby code:

```ruby
defined?(until a == 1; end)
```

Instructions:

```
"********* Ruby *************"
== disasm: #<ISeq:<compiled>@<compiled>:1 (1,0)-(59,16)>
0000 putobject                              "expression"              (  59)[Li]
0002 leave
"********* PRISM *************"
== disasm: #<ISeq:<compiled>@<compiled>:58 (58,0)-(58,16)>
0000 putobject                              "expression"              (  58)[Li]
0002 leave
```

Related: ruby/prism#2188
2024-01-17 17:21:29 -05:00
eileencodes 24d02cd485 [Prism] Implement defined? for PM_WHILE_NODE
Ruby code:

```ruby
defined?(while a != 1; end)
```

Instructions:

```
"********* Ruby *************"
== disasm: #<ISeq:<compiled>@<compiled>:1 (1,0)-(59,16)>
0000 putobject                              "expression"              (  59)[Li]
0002 leave
"********* PRISM *************"
== disasm: #<ISeq:<compiled>@<compiled>:58 (58,0)-(58,16)>
0000 putobject                              "expression"              (  58)[Li]
0002 leave
```

Related: ruby/prism#2188
2024-01-17 17:21:29 -05:00
eileencodes b5d3d61130 [Prism] Implement defined? for PM_SINGLETON_CLASS_NODE
Ruby code:

```ruby
defined?(class << self; end)
```

Instructions:

```
"********* Ruby *************"
== disasm: #<ISeq:<compiled>@<compiled>:1 (1,0)-(59,16)>
0000 putobject                              "expression"              (  59)[Li]
0002 leave
"********* PRISM *************"
== disasm: #<ISeq:<compiled>@<compiled>:58 (58,0)-(58,16)>
0000 putobject                              "expression"              (  58)[Li]
0002 leave
```

Related: ruby/prism#2188
2024-01-17 17:21:29 -05:00
eileencodes d2000e0e6c [Prism] Implement defined? for PM_RATIONAL_NODE
Ruby code:

```ruby
defined?(1.2r)
```

Instructions:

```
"********* Ruby *************"
== disasm: #<ISeq:<compiled>@<compiled>:1 (1,0)-(59,16)>
0000 putobject                              "expression"              (  59)[Li]
0002 leave
"********* PRISM *************"
== disasm: #<ISeq:<compiled>@<compiled>:58 (58,0)-(58,16)>
0000 putobject                              "expression"              (  58)[Li]
0002 leave
```

Related: ruby/prism#2188
2024-01-17 17:21:29 -05:00
eileencodes 913772a08a [Prism] Implement defined? for PM_MODULE_NODE
Ruby code:

```ruby
defined?(module M; end)
```

Instructions:

```
"********* Ruby *************"
== disasm: #<ISeq:<compiled>@<compiled>:1 (1,0)-(59,16)>
0000 putobject                              "expression"              (  59)[Li]
0002 leave
"********* PRISM *************"
== disasm: #<ISeq:<compiled>@<compiled>:58 (58,0)-(58,16)>
0000 putobject                              "expression"              (  58)[Li]
0002 leave
```

Related: ruby/prism#2188
2024-01-17 17:21:29 -05:00
eileencodes a9a22b9f2d [Prism] Implement defined? for PM_MATCH_REQUIRED_NODE
Ruby code:

```ruby
defined?(1 => 1)
```

Instructions:

```
"********* Ruby *************"
== disasm: #<ISeq:<compiled>@<compiled>:1 (1,0)-(59,16)>
0000 putobject                              "expression"              (  59)[Li]
0002 leave
"********* PRISM *************"
== disasm: #<ISeq:<compiled>@<compiled>:58 (58,0)-(58,16)>
0000 putobject                              "expression"              (  58)[Li]
0002 leave
```

Related: ruby/prism#2188
2024-01-17 17:21:29 -05:00
eileencodes b40fc4f0a7 [Prism] Implement defined? for PM_MATCH_WRITE_NODE
Ruby code:

```ruby
defined?(/(?<foo>bar)/ =~ 'barbar')
```

Instructions:

```
"********* Ruby *************"
== disasm: #<ISeq:<compiled>@<compiled>:1 (1,0)-(59,35)>
local table (size: 1, argc: 0 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] foo@0
0000 putobject                              "expression"              (  59)[Li]
0002 leave
"********* PRISM *************"
== disasm: #<ISeq:<compiled>@<compiled>:58 (58,0)-(58,35)>
local table (size: 1, argc: 0 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 1] foo@0
0000 putobject                              "expression"              (  58)[Li]
0002 leave
```

Related: ruby/prism#2188
2024-01-17 17:21:29 -05:00
eileencodes a2092ef207 [Prism] Implement defined? for PM_IF_NODE
Ruby code:

```ruby
defined?(if true; 1; end)
```

Instructions:

```
"********* Ruby *************"
== disasm: #<ISeq:<compiled>@<compiled>:1 (1,0)-(59,43)>
0000 putobject                              "expression"              (  59)[Li]
0002 leave
"********* PRISM *************"
== disasm: #<ISeq:<compiled>@<compiled>:58 (58,0)-(58,43)>
0000 putobject                              "expression"              (  58)[Li]
0002 leave
```

Related: ruby/prism#2188
2024-01-17 17:21:29 -05:00
eileencodes fb1eed3f70 [Prism] Implement defined? for PM_FOR_NODE
Ruby code:

```ruby
defined?(for i in [1,2] do; i; end)
```

Instructions:

```
"********* Ruby *************"
== disasm: #<ISeq:<compiled>@<compiled>:1 (1,0)-(59,43)>
0000 putobject                              "expression"              (  59)[Li]
0002 leave
"********* PRISM *************"
== disasm: #<ISeq:<compiled>@<compiled>:58 (58,0)-(58,43)>
0000 putobject                              "expression"              (  58)[Li]
0002 leave
```

Related: ruby/prism#2188
2024-01-17 17:21:29 -05:00
eileencodes 7460820fec [Prism] Implement defined? for PM_DEF_NODE
Ruby code:

```ruby
defined?(def prism_test_def_node; end)
```

Instructions:

```
"********* Ruby *************"
== disasm: #<ISeq:<compiled>@<compiled>:1 (1,0)-(59,43)>
0000 putobject                              "expression"              (  59)[Li]
0002 leave
"********* PRISM *************"
== disasm: #<ISeq:<compiled>@<compiled>:58 (58,0)-(58,43)>
0000 putobject                              "expression"              (  58)[Li]
0002 leave
```

Related: ruby/prism#2188
2024-01-17 17:21:29 -05:00
eileencodes 30e7dbb78d [Prism] Implement defined? for PM_CLASS_NODE
Ruby code:

```ruby
defined?(class PrismClassA; end)
```

Instructions:

```
"********* Ruby *************"
== disasm: #<ISeq:<compiled>@<compiled>:1 (1,0)-(59,43)>
0000 putobject                              "expression"              (  59)[Li]
0002 leave
"********* PRISM *************"
== disasm: #<ISeq:<compiled>@<compiled>:58 (58,0)-(58,43)>
0000 putobject                              "expression"              (  58)[Li]
0002 leave
```

Related: ruby/prism#2188
2024-01-17 17:21:29 -05:00
eileencodes 9bcd42c378 [Prism] Implement defined? for PM_CASE_MATCH_NODE
Ruby code:

```ruby
defined?(case [1, 2, 3]; in [1, 2, 3]; 4; end)
```

Instructions:

```
"********* Ruby *************"
== disasm: #<ISeq:<compiled>@<compiled>:1 (1,0)-(59,43)>
0000 putobject                              "expression"              (  59)[Li]
0002 leave
"********* PRISM *************"
== disasm: #<ISeq:<compiled>@<compiled>:58 (58,0)-(58,43)>
0000 putobject                              "expression"              (  58)[Li]
0002 leave
```

Related: ruby/prism#2188
2024-01-17 17:21:29 -05:00