Skip to content

Commit

Permalink
Add back accidentally-removed JS [[ToString]] operation (`Value::to_s…
Browse files Browse the repository at this point in the history
…tring()`)
  • Loading branch information
dherman committed Mar 9, 2019
1 parent 749cd6f commit aea5dc0
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
18 changes: 10 additions & 8 deletions crates/neon-runtime/src/neon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -219,14 +219,16 @@ extern "C" size_t Neon_String_Data(char *out, size_t len, v8::Persistent<v8::Str
return Nan::DecodeWrite(out, len, local, Nan::UTF8);
}

extern "C" bool Neon_Convert_ToString(v8::Local<v8::String> *out, v8::Local<v8::Value> value) {
Nan::MaybeLocal<v8::String> maybe = Nan::To<v8::String>(value);
return maybe.ToLocal(out);
}

extern "C" bool Neon_Convert_ToObject(v8::Local<v8::Object> *out, v8::Local<v8::Value> *value) {
Nan::MaybeLocal<v8::Object> maybe = Nan::To<v8::Object>(*value);
return maybe.ToLocal(out);
extern "C" bool Neon_String_ToString(v8::Persistent<v8::String> *out, v8::Isolate *isolate, v8::Persistent<v8::Value> *value) {
Nan::HandleScope scope;
v8::Local<v8::Value> lvalue = Nan::New(*value);
Nan::MaybeLocal<v8::String> maybe = Nan::To<v8::String>(lvalue);
v8::Local<v8::String> lout;
if (!maybe.ToLocal(&lout)) {
return false;
}
out->Reset(isolate, lout);
return true;
}

extern "C" bool Neon_Buffer_New(v8::Persistent<v8::Value> *out, v8::Isolate *isolate, uint32_t len) {
Expand Down
4 changes: 1 addition & 3 deletions crates/neon-runtime/src/neon.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ extern "C" {
bool Neon_String_New(v8::Persistent<v8::String> *out, v8::Isolate *isolate, const uint8_t *data, int32_t len);
int32_t Neon_String_Utf8Length(v8::Persistent<v8::String> *str);
size_t Neon_String_Data(char *out, size_t len, v8::Persistent<v8::String> *str);

bool Neon_Convert_ToString(v8::Local<v8::String> *out, v8::Local<v8::Value> value);
bool Neon_Convert_ToObject(v8::Local<v8::Object> *out, v8::Local<v8::Value> *value);
bool Neon_String_ToString(v8::Persistent<v8::String> *out, v8::Isolate *isolate, v8::Persistent<v8::Value> *value);

bool Neon_Buffer_New(v8::Persistent<v8::Value> *out, v8::Isolate *isolate, uint32_t len);
bool Neon_Buffer_Uninitialized(v8::Persistent<v8::Value> *out, v8::Isolate *isolate, uint32_t len);
Expand Down
5 changes: 5 additions & 0 deletions crates/neon-runtime/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ extern "C" {
#[link_name = "Neon_String_Data"]
pub fn data(out: *mut u8, len: isize, str: &Persistent) -> isize;

/// Mutates the `out` argument to the result of applying the standard JS string conversion
/// operation on the `from` argument.
#[link_name = "Neon_String_ToString"]
pub fn to_string(out: &Persistent, isolate: *mut Isolate, from: &Persistent) -> bool;

}
5 changes: 5 additions & 0 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ pub trait Value: ValueInternal {
self.downcast().or_throw(cx)
}

fn to_string<'a, C: Context<'a>>(&self, cx: &mut C) -> NeonResult<&'a JsString> {
cx.new(|out, isolate| unsafe {
neon_runtime::string::to_string(out, isolate, self.to_raw())
})
}
}

/// An error representing a failed downcast.
Expand Down

0 comments on commit aea5dc0

Please sign in to comment.