Comments (4)
The issue is that the typed arrays alias. Element 0 of the 32-bit heap aliases the same data as the first 4 elements of the 8-bit heap, etc. This is necessary for C and C++ compatibility, and luckily JS typed arrays are capable of it.
Given that aliasing, note that we can only read a 32-bit value at addresses 0, 4, 8, etc. - since the first element is bytes 0-3, second is 4-7, etc. In other words, the pointer must be aligned: 32-bit values are read from addresses that are a multiple of 32 bits.
The shift enforces that: a 32-bit read of ptr >> 2
means that we read from address ptr
, but we force it to be aligned. If ptr
is 4, then we read from element 1, which aliases bytes 4-7, etc. But if we try to do an unaligned read from 5, we also read from the same location, as the shift removes the unaligned bits.
from validator.
So it's behaving as if ptr
were assumed to be a byte offset of a raw buffer, rather than an array index? So I can write my "pointers" as offsets like so and save myself a few characters:
...
var P_MYVAR = 0;
var P_MYVAR2 = 4;
...
heap32[P_MYVAR >>2] = ...
What's the advantage of doing it this way vs. just expecting the transpiler to do the math, compile-time performance? I guess people aren't expected to write this by hand very often.
from validator.
Yes, ptr
is just like a C pointer - it's the actual address.
Doing the shift on each load/store ensures alignment as mentioned before. It also is efficient since when generating machine code you want the actual address, not the JS typed array index.
from validator.
Gotcha. Thanks for the help :)
from validator.
Related Issues (20)
- Request: Fast integer overflow checks HOT 1
- is this repository maintained? HOT 4
- Accept comma-separated function table declarations HOT 2
- Command line utility? HOT 1
- Validator accepts code that it shouldn't HOT 4
- congrats HOT 1
- Allow constants in case blocks
- undefined cannot "flow freely" but can "escape"
- Spec allows standard library functions to be exported HOT 1
- Heap indexing expressions should be subtypes of intish
- Fround annotations break hoisting semantics HOT 1
- If doesn't work? HOT 4
- Mentioned on The Changelog "#189: JSON API with Yehuda Katz"
- Assignment operators are not validated HOT 1
- Foreign function invocations do not validate HOT 1
- Consider allowing x|=0 as a valid Parameter Type Annotation
- anything with fround doesn't validate
- Warning Chrome : Invalid asm.js: Unexpected token
- Grammar incorrect in document
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from validator.