Giter VIP home page Giter VIP logo

Comments (4)

kripken avatar kripken commented on July 18, 2024

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.

nphyx avatar nphyx commented on July 18, 2024

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.

kripken avatar kripken commented on July 18, 2024

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.

nphyx avatar nphyx commented on July 18, 2024

Gotcha. Thanks for the help :)

from validator.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.