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

ArrayBuffer #210

Merged
merged 4 commits into from
May 28, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -5,4 +5,5 @@ Cargo.lock
**/.DS_Store
**/build
**/index.node
**/artifacts.json
npm-debug.log
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -17,6 +17,9 @@ build = "build.rs"
# See also: http://doc.crates.io/build-script.html#the-links-manifest-key
links = "neon-runtime"

[build-dependencies]
neon-build = { version = "=0.1.17", path = "crates/neon-build" }

[dependencies]
cslice = "0.2"
neon-runtime = { version = "=0.1.17", path = "crates/neon-runtime" }
12 changes: 8 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -6,17 +6,21 @@ all: doc
publish: doc
cd target/doc && surge

doc: neon_api neon_sys_api $(HTML_FILES) target/doc/rust.css target/doc/CNAME
doc: neon_api neon_build_api neon_runtime_api $(HTML_FILES) target/doc/rust.css target/doc/CNAME

clean:
rm -rf target/doc

neon_api:
cargo doc

neon_sys_api:
cd crates/neon-sys && cargo doc
cp -R crates/neon-sys/target/doc/neon_sys ./target/doc/
neon_build_api:
cd crates/neon_build && cargo doc
cp -R crates/neon-build/target/doc/neon_build ./target/doc

neon_runtime_api:
cd crates/neon-runtime && cargo doc
cp -R crates/neon-runtime/target/doc/neon_runtime ./target/doc/

target/doc/%.html: doc/%.md
rustdoc --markdown-playground-url='https://play.rust-lang.org' --markdown-css rust.css $< --output=target/doc
20 changes: 20 additions & 0 deletions crates/neon-runtime/src/arraybuffer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//! Facilities for working with `v8::ArrayBuffer`s.

use raw::Local;
use cslice::CMutSlice;
use std::os::raw::c_void;

// Suppress a spurious rustc warning about the use of CMutSlice.
#[allow(improper_ctypes)]
extern "C" {

/// Mutates the `out` argument provided to refer to a newly created `v8::ArrayBuffer` object.
/// Returns `false` if the value couldn't be created.
#[link_name = "Neon_ArrayBuffer_New"]
pub fn new(out: &mut Local, isolate: *mut c_void, size: u32) -> bool;

/// Mutates the `out` argument provided populating the `data` and `len` properties.
#[link_name = "Neon_ArrayBuffer_Data"]
pub fn data<'a, 'b>(out: &'a mut CMutSlice<'b, u8>, obj: Local);

}
1 change: 1 addition & 0 deletions crates/neon-runtime/src/lib.rs
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ pub mod array;
pub mod string;
pub mod primitive;
pub mod error;
pub mod arraybuffer;
pub mod buffer;
pub mod tag;
pub mod module;
15 changes: 15 additions & 0 deletions crates/neon-runtime/src/neon.cc
Original file line number Diff line number Diff line change
@@ -201,6 +201,21 @@ extern "C" bool Neon_Tag_IsBuffer(v8::Local<v8::Value> obj) {
return node::Buffer::HasInstance(obj);
}

extern "C" bool Neon_ArrayBuffer_New(v8::Local<v8::ArrayBuffer> *out, v8::Isolate *isolate, uint32_t size) {
*out = v8::ArrayBuffer::New(isolate, size);
return true;
}

extern "C" void Neon_ArrayBuffer_Data(buf_t *out, v8::Local<v8::ArrayBuffer> buffer) {
v8::ArrayBuffer::Contents contents = buffer->GetContents();
out->data = contents.Data();
out->len = contents.ByteLength();
}

extern "C" bool Neon_Tag_IsArrayBuffer(v8::Local<v8::Value> value) {
return value->IsArrayBuffer();
}

extern "C" void Neon_Scope_Escape(v8::Local<v8::Value> *out, Nan::EscapableHandleScope *scope, v8::Local<v8::Value> value) {
*out = scope->Escape(value);
}
4 changes: 4 additions & 0 deletions crates/neon-runtime/src/neon.h
Original file line number Diff line number Diff line change
@@ -68,6 +68,9 @@ extern "C" {
bool Neon_Buffer_New(v8::Local<v8::Object> *out, uint32_t size);
void Neon_Buffer_Data(buf_t *out, v8::Local<v8::Object> obj);

bool Neon_ArrayBuffer_New(v8::Local<v8::ArrayBuffer> *out, v8::Isolate *isolate, uint32_t size);
void Neon_ArrayBuffer_Data(buf_t *out, v8::Local<v8::ArrayBuffer> buffer);

typedef void(*Neon_ChainedScopeCallback)(void *, void *, void *, void *);
typedef void(*Neon_NestedScopeCallback)(void *, void *, void *);
typedef void(*Neon_RootScopeCallback)(void *, void *, void *);
@@ -132,6 +135,7 @@ extern "C" {
bool Neon_Tag_IsArray(v8::Local<v8::Value> val);
bool Neon_Tag_IsFunction(v8::Local<v8::Value> val);
bool Neon_Tag_IsBuffer(v8::Local<v8::Value> obj);
bool Neon_Tag_IsArrayBuffer(v8::Local<v8::Value> obj);
bool Neon_Tag_IsError(v8::Local<v8::Value> val);

void Neon_Error_NewError(v8::Local<v8::Value> *out, v8::Local<v8::String> msg);
4 changes: 4 additions & 0 deletions crates/neon-runtime/src/tag.rs
Original file line number Diff line number Diff line change
@@ -68,4 +68,8 @@ extern "C" {
#[link_name = "Neon_Tag_IsBuffer"]
pub fn is_buffer(obj: Local) -> bool;

/// Indicates if the value type is `ArrayBuffer`.
#[link_name = "Neon_Tag_IsArrayBuffer"]
pub fn is_arraybuffer(obj: Local) -> bool;

}
37 changes: 37 additions & 0 deletions src/internal/js/binary.rs
Original file line number Diff line number Diff line change
@@ -44,3 +44,40 @@ impl<'a> Lock for &'a mut JsBuffer {
result
}
}

#[repr(C)]
#[derive(Clone, Copy)]
pub struct JsArrayBuffer(raw::Local);

impl JsArrayBuffer {
pub fn new<'a, T: Scope<'a>>(scope: &mut T, size: u32) -> VmResult<Handle<'a, JsArrayBuffer>> {
build(|out| { unsafe { neon_runtime::arraybuffer::new(out, mem::transmute(scope.isolate()), size) } })
}
}

impl Managed for JsArrayBuffer {
fn to_raw(self) -> raw::Local { self.0 }

fn from_raw(h: raw::Local) -> Self { JsArrayBuffer(h) }
}

impl ValueInternal for JsArrayBuffer {
fn is_typeof<Other: Value>(other: Other) -> bool {
unsafe { neon_runtime::tag::is_arraybuffer(other.to_raw()) }
}
}

impl Value for JsArrayBuffer { }

impl Object for JsArrayBuffer { }

impl<'a> Lock for &'a mut JsArrayBuffer {
type Internals = CMutSlice<'a, u8>;

unsafe fn expose(self, state: &mut LockState) -> Self::Internals {
let mut result = mem::uninitialized();
neon_runtime::arraybuffer::data(&mut result, self.to_raw());
state.use_buffer(result);
result
}
}
2 changes: 1 addition & 1 deletion src/js/binary.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub use internal::js::binary::JsBuffer;
pub use internal::js::binary::{JsBuffer, JsArrayBuffer};
6 changes: 3 additions & 3 deletions tests/package.json
Original file line number Diff line number Diff line change
@@ -6,11 +6,11 @@
"author": "The Neon Community",
"license": "MIT",
"dependencies": {
"neon-cli": "^0.1.7"
"neon-cli": "git://github.com/neon-bindings/neon-cli"
},
"scripts": {
"install": "neon build",
"test": "npm i && mocha --recursive lib"
"install": "cd node_modules/neon-cli && npm i && npm run transpile",
"test": "npm i && neon build && mocha --recursive lib"
},
"devDependencies": {
"chai": "^3.5.0",