-
Notifications
You must be signed in to change notification settings - Fork 323
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix outdated V8 Torque builtins #762
Open
rluvaton
wants to merge
4
commits into
v8:main
Choose a base branch
from
rluvaton:patch-1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,50 +28,52 @@ This example demonstrates: | |
- Using Torque to implement simple logic: type distinction, Smi and heap-number handling, conditionals. | ||
- Installation of the CSA builtin on the `Math` object. | ||
|
||
In case you’d like to follow along locally, the following code is based off revision [589af9f2](https://chromium.googlesource.com/v8/v8/+/589af9f257166f66774b4fb3008cd09f192c2614). | ||
In case you’d like to follow along locally, the following code is based off revision [5e0cc470](https://chromium.googlesource.com/v8/v8/+/5e0cc470deed2d3b0957b7441d1e960313bbbf2d). | ||
|
||
## Defining `MathIs42` | ||
|
||
Torque code is located in `src/builtins/*.tq` files, roughly organized by topic. Since we will be writing a `Math` builtin, we’ll put our definition into `src/builtins/math.tq`. Since this file doesn't exist yet, we have to add it to [`torque_files`](https://cs.chromium.org/chromium/src/v8/BUILD.gn?l=914&rcl=589af9f257166f66774b4fb3008cd09f192c2614) in [`BUILD.gn`](https://cs.chromium.org/chromium/src/v8/BUILD.gn). | ||
Torque code is located in `src/builtins/*.tq` files, roughly organized by topic. Since we will be writing a `Math` builtin, we’ll put our definition into `src/builtins/math.tq`. | ||
This file already exists, but in case you want to add new file you have to add it to [`torque_files`](https://source.chromium.org/chromium/v8/v8.git/+/5e0cc470deed2d3b0957b7441d1e960313bbbf2d:BUILD.gn;l=1892) in [`BUILD.gn`](https://cs.chromium.org/chromium/src/v8/BUILD.gn). | ||
|
||
```torque | ||
// Existing code to set up Math, included here for clarity. | ||
namespace math { | ||
javascript builtin MathIs42( | ||
context: Context, receiver: Object, x: Object): Boolean { | ||
// At this point, x can be basically anything - a Smi, a HeapNumber, | ||
// undefined, or any other arbitrary JS object. ToNumber_Inline is defined | ||
// in CodeStubAssembler. It inlines a fast-path (if the argument is a number | ||
// already) and calls the ToNumber builtin otherwise. | ||
const number: Number = ToNumber_Inline(x); | ||
// A typeswitch allows us to switch on the dynamic type of a value. The type | ||
// system knows that a Number can only be a Smi or a HeapNumber, so this | ||
// switch is exhaustive. | ||
typeswitch (number) { | ||
case (smi: Smi): { | ||
// The result of smi == 42 is not a Javascript boolean, so we use a | ||
// conditional to create a Javascript boolean value. | ||
return smi == 42 ? True : False; | ||
// […snip…] | ||
transitioning javascript builtin MathIs42( | ||
js-implicit context: NativeContext)(x: JSAny): Boolean { | ||
// At this point, x can be basically anything - a Smi, a HeapNumber, | ||
// undefined, or any other arbitrary JS object. ToNumber_Inline is defined | ||
// in CodeStubAssembler. It inlines a fast-path (if the argument is a number | ||
// already) and calls the ToNumber builtin otherwise. | ||
const number: Number = ToNumber_Inline(x); | ||
// A typeswitch allows us to switch on the dynamic type of a value. The type | ||
// system knows that a Number can only be a Smi or a HeapNumber, so this | ||
// switch is exhaustive. | ||
typeswitch (number) { | ||
case (smi: Smi): { | ||
// The result of smi == 42 is not a Javascript boolean, so we use a | ||
// conditional to create a Javascript boolean value. | ||
return smi == 42 ? True : False; | ||
} | ||
case (heapNumber: HeapNumber): { | ||
return Convert<float64>(heapNumber) == 42 ? True : False; | ||
} | ||
} | ||
case (heapNumber: HeapNumber): { | ||
return Convert<float64>(heapNumber) == 42 ? True : False; | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
|
||
We put the definition in the Torque namespace `math`. Since this namespace didn't exist before, we have to add it to [`torque_namespaces`](https://cs.chromium.org/chromium/src/v8/BUILD.gn?l=933&rcl=589af9f257166f66774b4fb3008cd09f192c2614) in [`BUILD.gn`](https://cs.chromium.org/chromium/src/v8/BUILD.gn). | ||
|
||
## Attaching `Math.is42` | ||
|
||
Builtin objects such as `Math` are set up mostly in [`src/bootstrapper.cc`](https://cs.chromium.org/chromium/src/v8/src/bootstrapper.cc?q=src/bootstrapper.cc+package:%5Echromium$&l=1) (with some setup occurring in `.js` files). Attaching our new builtin is simple: | ||
Builtin objects such as `Math` are set up mostly in [`src/init/bootstrapper.cc`](https://cs.chromium.org/chromium/src/v8/src/init/bootstrapper.cc?q=src/init/bootstrapper.cc+package:%5Echromium$&l=1) (with some setup occurring in `.js` files). Attaching our new builtin is simple: | ||
|
||
```cpp | ||
// Existing code to set up Math, included here for clarity. | ||
Handle<JSObject> math = factory->NewJSObject(cons, TENURED); | ||
JSObject::AddProperty(global, name, math, DONT_ENUM); | ||
// -- M a t h | ||
Handle<JSObject> math = factory->NewJSObject(isolate_->object_function(), AllocationType::kOld); | ||
JSObject::AddProperty(global, "Math", math, DONT_ENUM); | ||
// […snip…] | ||
SimpleInstallFunction(isolate_, math, "is42", Builtins::kMathIs42, 1, true); | ||
SimpleInstallFunction(isolate_, math, "is42", Builtin::kMathIs42, 1, true); | ||
``` | ||
|
||
Now that `is42` is attached, it can be called from JS: | ||
|
@@ -96,13 +98,11 @@ The definition is also straightforward. The only difference to our builtin with | |
|
||
```torque | ||
namespace math { | ||
builtin HeapNumberIs42(implicit context: Context)(heapNumber: HeapNumber): | ||
Boolean { | ||
transitioning macro HeapNumberIs42(implicit context: Context)(heapNumber: HeapNumber): Boolean { | ||
return Convert<float64>(heapNumber) == 42 ? True : False; | ||
} | ||
|
||
javascript builtin MathIs42(implicit context: Context)( | ||
receiver: Object, x: Object): Boolean { | ||
transitioning javascript builtin MathIs42(js-implicit context: NativeContext)(x: JSAny): Boolean { | ||
const number: Number = ToNumber_Inline(x); | ||
typeswitch (number) { | ||
case (smi: Smi): { | ||
|
@@ -132,7 +132,7 @@ TEST(MathIsHeapNumber42) { | |
Heap* heap = isolate->heap(); | ||
Zone* zone = scope.main_zone(); | ||
|
||
StubTester tester(isolate, zone, Builtins::kMathIs42); | ||
StubTester tester(isolate, zone, Builtin::kMathIs42); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line 126 should be updated as well, there is no more |
||
Handle<Object> result1 = tester.Call(Handle<Smi>(Smi::FromInt(0), isolate)); | ||
CHECK(result1->BooleanValue()); | ||
} | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lines 95-97 should be updated as well but I'm not sure what to write there