Skip to content
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

feat: Implement the WasmArrayBuffer class #35

Merged
merged 28 commits into from
Mar 26, 2019
Merged

Conversation

Hywan
Copy link
Contributor

@Hywan Hywan commented Mar 25, 2019

Implement WasmArrayBuffer and WasmTypedArray classes i.e. WasmInt8Array, WasmUint8Array, WasmInt16Array, WasmUint16Array, WasmInt32Array and WasmUint32Array.

Prototype:

final class WasmArrayBuffer
{
    public function __construct(int $byte_length);
    public function getByteLength(): int;
}

final class WasmUint8Array implements ArrayAccess
{
    public const BYTES_PER_ELEMENT;
    
    public function __construct(WasmArrayBuffer $wasm_array_buffer, int $offset = 0, int $length = 0);
    public function getOffset(): int;
    public function getLength(): int;

    /* For `ArrayAccess` */
    public function offsetGet($offset): int;
    public function offsetSet($offset, $value): void;
    public function offsetExists($offset): bool;
    public function offsetUnset($offset): void;
}

Example:

$wasmArrayBuffer = new WasmArrayBuffer(256);
$int8 = new WasmInt8Array($wasmArrayBuffer);
$int16 = new WasmInt16Array($wasmArrayBuffer);
$int32 = new WasmInt32Array($wasmArrayBuffer);

                b₁
             ┌┬┬┬┬┬┬┐
$int8[0] = 0b00000001;
                b₂
             ┌┬┬┬┬┬┬┐
$int8[1] = 0b00000100;
                b₃
             ┌┬┬┬┬┬┬┐
$int8[2] = 0b00010000;
                b₄
             ┌┬┬┬┬┬┬┐
$int8[3] = 0b01000000;

// No surprise with the following assertions.
                         b₁
                      ┌┬┬┬┬┬┬┐
assert($int8[0] === 0b00000001);
                         b₂
                      ┌┬┬┬┬┬┬┐
assert($int8[1] === 0b00000100);
                         b₃
                      ┌┬┬┬┬┬┬┐
assert($int8[2] === 0b00010000);
                         b₄
                      ┌┬┬┬┬┬┬┐
assert($int8[3] === 0b01000000);

// The `int16` view read 2 bytes.
                          b₂      b₁
                       ┌┬┬┬┬┬┬┐┌┬┬┬┬┬┬┐
assert($int16[0] === 0b0000010000000001);
                          b₄      b₃
                       ┌┬┬┬┬┬┬┐┌┬┬┬┬┬┬┐
assert($int16[1] === 0b0100000000010000);

// The `int32` view reads 4 bytes.
                          b₄      b₃      b₂      b₁
                       ┌┬┬┬┬┬┬┐┌┬┬┬┬┬┬┐┌┬┬┬┬┬┬┐┌┬┬┬┬┬┬┐
assert($int32[0] === 0b01000000000100000000010000000001);

The code is highly inspired by http://www.phpinternalsbook.com/classes_objects/implementing_typed_arrays.html, with bug fixes and written for Zend Engine 3.

@Hywan Hywan added 🎉 enhancement New feature or request 🧪 tests I love tests 📦 component-extension About the PHP extension written in C/C++ labels Mar 25, 2019
@Hywan Hywan self-assigned this Mar 25, 2019
Hywan added 21 commits March 25, 2019 13:39
Using `calloc` instead of `malloc` alows to initialize the buffer with
zero bytes, which is better for the security: The buffer won't allow
to read existing in-memory data.
@Hywan Hywan marked this pull request as ready for review March 26, 2019 11:05
@Hywan Hywan added the 📚 documentation Do you like to read? label Mar 26, 2019
@Ocramius
Copy link

@Hywan just a note on the OP:

  • classes should be final (extending from extensions is reeeeeally bad)
  • add : void where possible
  • type declarations for getOffset() and getLength()?

lib/README.md Outdated Show resolved Hide resolved
lib/README.md Show resolved Hide resolved
lib/README.md Outdated Show resolved Hide resolved
lib/README.md Outdated Show resolved Hide resolved
lib/README.md Outdated Show resolved Hide resolved
lib/README.md Show resolved Hide resolved
tests/units/Extension/Constants.php Show resolved Hide resolved
tests/units/Extension/Classes.php Outdated Show resolved Hide resolved
lib/README.md Outdated Show resolved Hide resolved
lib/README.md Outdated Show resolved Hide resolved
@Hywan
Copy link
Contributor Author

Hywan commented Mar 26, 2019

Thanks @Ocramius for the review!

@Hywan Hywan merged commit 0a108d9 into master Mar 26, 2019
@Hywan Hywan deleted the feat-extension-arraybuffer branch April 3, 2019 09:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📦 component-extension About the PHP extension written in C/C++ 📚 documentation Do you like to read? 🎉 enhancement New feature or request 🧪 tests I love tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants