Skip to content

Bigint (take 2, AI aided)#2776

Draft
elia wants to merge 7 commits intomasterfrom
elia/bigint-take-2
Draft

Bigint (take 2, AI aided)#2776
elia wants to merge 7 commits intomasterfrom
elia/bigint-take-2

Conversation

@elia
Copy link
Copy Markdown
Member

@elia elia commented Dec 26, 2025

No description provided.

elia added 4 commits December 24, 2025 10:45
Add a new compiler option :bigint_integers (default: false) that maps
Ruby Integer literals to JavaScript BigInt (e.g., 42 -> 42n) instead of
Number. This enables gradual migration to BigInt for better Integer
semantics.

The option can be enabled via:
- Compiler option: Opal.compile(code, bigint_integers: true)
- Magic comment: # bigint_integers: true

Float literals remain unchanged regardless of this setting.
Document the phased approach to migrate from JavaScript Number to BigInt
for representing Ruby Integer literals throughout the codebase.
When bigint_integers option is enabled, transform integer literals
inside %x{} blocks (backtick JavaScript) to BigInt notation.

This allows existing corelib code using embedded JS to work correctly
with BigInt without manual rewriting.

Transformations:
- Decimal: 42 -> 42n
- Hex: 0xFF -> 0xFFn
- Octal: 0o77 -> 0o77n
- Binary: 0b1010 -> 0b1010n

Preserved:
- Floats: 3.14 stays 3.14 (not 3.14n)
- Already BigInt: 123n stays 123n (not 123nn)
- Decimal parts: 3.14 doesn't transform the 14
Keep the default as false to maintain backward compatibility with
existing tests and code. The option can be explicitly enabled when
ready.

This allows gradual adoption and testing of BigInt support before
making it the default behavior.
@elia elia self-assigned this Dec 26, 2025
Comment thread lib/opal/nodes/x_string.rb Outdated
# But NOT if:
# - already followed by 'n' (BigInt) or '.' (Float)
# - preceded by '.' (decimal part of a float)
js_code.gsub(/(?<!\.)(\b(?:0[xX][0-9a-fA-F]+|0[oO][0-7]+|0[bB][01]+|\d+))(?![n.\d])/) do
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting approach, but I'm afraid it will transmute numbers inside strings.

Though I'd rather just mandate using #{1} inside x-strings.

elia added 3 commits December 29, 2025 09:50
Remove automatic transformation of integers in embedded JavaScript (%x{}).
This simplifies the implementation and avoids complex parsing issues.

Strategy:
- Ruby Integer literals become BigInt (42 -> 42n)
- JS embedded code stays as-is (backticks use Number)
- Runtime must handle mixed Number/BigInt operations

This is safer and lets us focus on runtime support for mixed operations
rather than trying to parse and transform JavaScript code.
Document completed work and identified blockers:
- Compiler support complete (bigint_integers option)
- Runtime operators need mixed Number/BigInt support
- Next step: fix op_helpers.rb to handle both types
- Architectural decision: don't transform JS embedded code
No Italian content should remain in the repository.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants