Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

Commit

Permalink
Merge branch 'noexceptions'
Browse files Browse the repository at this point in the history
  • Loading branch information
saper committed Sep 10, 2015
2 parents c5a16a9 + 228b39d commit 43286a1
Show file tree
Hide file tree
Showing 20 changed files with 146 additions and 61 deletions.
12 changes: 1 addition & 11 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,12 @@
'-std=c++11'
],
'OTHER_LDFLAGS': [],
'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',
'GCC_ENABLE_CPP_EXCEPTIONS': 'NO',
'MACOSX_DEPLOYMENT_TARGET': '10.7'
}
}],
['OS=="win"', {
'msvs_settings': {
'VCCLCompilerTool': {
'AdditionalOptions': [
'/EHsc'
]
}
}
}],
['OS!="win"', {
'cflags_cc+': [
'-fexceptions',
'-std=c++0x'
]
}]
Expand Down
7 changes: 1 addition & 6 deletions src/binding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,7 @@ union Sass_Value* sass_custom_function(const union Sass_Value* s_args, Sass_Func
argv.push_back((void*)sass_list_get_value(s_args, i));
}

try {
return bridge(argv);
}
catch (const std::exception& e) {
return sass_make_error(e.what());
}
return bridge(argv);
}

int ExtractOptions(v8::Local<v8::Object> options, void* cptr, sass_context_wrapper* ctx_w, bool is_file, bool is_sync) {
Expand Down
8 changes: 8 additions & 0 deletions src/callback_bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,12 @@ T CallbackBridge<T, L>::operator()(std::vector<void*> argv) {
* post_process_args().
*/
Nan::HandleScope scope;
Nan::TryCatch try_catch;
std::vector<v8::Local<v8::Value>> argv_v8 = pre_process_args(argv);
if (try_catch.HasCaught()) {
Nan::FatalException(try_catch);
}

argv_v8.push_back(Nan::New(wrapper));

return this->post_process_return_value(
Expand Down Expand Up @@ -149,6 +154,9 @@ void CallbackBridge<T, L>::dispatched_async_uv_callback(uv_async_t *req) {
Nan::TryCatch try_catch;

std::vector<v8::Local<v8::Value>> argv_v8 = bridge->pre_process_args(bridge->argv);
if (try_catch.HasCaught()) {
Nan::FatalException(try_catch);
}
argv_v8.push_back(Nan::New(bridge->wrapper));

bridge->callback->Call(argv_v8.size(), &argv_v8[0]);
Expand Down
11 changes: 6 additions & 5 deletions src/custom_function_bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
#include <stdexcept>
#include "custom_function_bridge.h"
#include "sass_types/factory.h"
#include "sass_types/value.h"

Sass_Value* CustomFunctionBridge::post_process_return_value(v8::Local<v8::Value> val) const {
try {
return SassTypes::Factory::unwrap(val)->get_sass_value();
}
catch (const std::invalid_argument& e) {
return sass_make_error(e.what());
SassTypes::Value *v_;
if ((v_ = SassTypes::Factory::unwrap(val))) {
return v_->get_sass_value();
} else {
return sass_make_error("A SassValue object was expected.");
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/sass_types/boolean.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ namespace SassTypes
}

NAN_METHOD(Boolean::GetValue) {
info.GetReturnValue().Set(Nan::New(static_cast<Boolean*>(Factory::unwrap(info.This()))->value));
Boolean *out;
if ((out = static_cast<Boolean*>(Factory::unwrap(info.This())))) {
info.GetReturnValue().Set(Nan::New(out->value));
}
}
}
12 changes: 6 additions & 6 deletions src/sass_types/color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ namespace SassTypes
{
Color::Color(Sass_Value* v) : SassValueWrapper(v) {}

Sass_Value* Color::construct(const std::vector<v8::Local<v8::Value>> raw_val) {
Sass_Value* Color::construct(const std::vector<v8::Local<v8::Value>> raw_val, Sass_Value **out) {
double a = 1.0, r = 0, g = 0, b = 0;
unsigned argb;

switch (raw_val.size()) {
case 1:
if (!raw_val[0]->IsNumber()) {
throw std::invalid_argument("Only argument should be an integer.");
return fail("Only argument should be an integer.", out);
}

argb = Nan::To<int32_t>(raw_val[0]).FromJust();
Expand All @@ -24,15 +24,15 @@ namespace SassTypes

case 4:
if (!raw_val[3]->IsNumber()) {
throw std::invalid_argument("Constructor arguments should be numbers exclusively.");
return fail("Constructor arguments should be numbers exclusively.", out);
}

a = Nan::To<double>(raw_val[3]).FromJust();
// fall through vvv

case 3:
if (!raw_val[0]->IsNumber() || !raw_val[1]->IsNumber() || !raw_val[2]->IsNumber()) {
throw std::invalid_argument("Constructor arguments should be numbers exclusively.");
return fail("Constructor arguments should be numbers exclusively.", out);
}

r = Nan::To<double>(raw_val[0]).FromJust();
Expand All @@ -44,10 +44,10 @@ namespace SassTypes
break;

default:
throw std::invalid_argument("Constructor should be invoked with either 0, 1, 3 or 4 arguments.");
return fail("Constructor should be invoked with either 0, 1, 3 or 4 arguments.", out);
}

return sass_make_color(r, g, b, a);
return *out = sass_make_color(r, g, b, a);
}

void Color::initPrototype(v8::Local<v8::FunctionTemplate> proto) {
Expand Down
2 changes: 1 addition & 1 deletion src/sass_types/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace SassTypes
public:
Color(Sass_Value*);
static char const* get_constructor_name() { return "SassColor"; }
static Sass_Value* construct(const std::vector<v8::Local<v8::Value>>);
static Sass_Value* construct(const std::vector<v8::Local<v8::Value>>, Sass_Value **);

static void initPrototype(v8::Local<v8::FunctionTemplate>);

Expand Down
6 changes: 3 additions & 3 deletions src/sass_types/error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ namespace SassTypes
{
Error::Error(Sass_Value* v) : SassValueWrapper(v) {}

Sass_Value* Error::construct(const std::vector<v8::Local<v8::Value>> raw_val) {
Sass_Value* Error::construct(const std::vector<v8::Local<v8::Value>> raw_val, Sass_Value **out) {
char const* value = "";

if (raw_val.size() >= 1) {
if (!raw_val[0]->IsString()) {
throw std::invalid_argument("Argument should be a string.");
return fail("Argument should be a string.", out);
}

value = create_string(raw_val[0]);
}

return sass_make_error(value);
return *out = sass_make_error(value);
}

void Error::initPrototype(v8::Local<v8::FunctionTemplate>) {}
Expand Down
2 changes: 1 addition & 1 deletion src/sass_types/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace SassTypes
public:
Error(Sass_Value*);
static char const* get_constructor_name() { return "SassError"; }
static Sass_Value* construct(const std::vector<v8::Local<v8::Value>>);
static Sass_Value* construct(const std::vector<v8::Local<v8::Value>>, Sass_Value **);

static void initPrototype(v8::Local<v8::FunctionTemplate>);
};
Expand Down
6 changes: 4 additions & 2 deletions src/sass_types/factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ namespace SassTypes
return new Error(v);

default:
throw std::invalid_argument("Unknown type encountered.");
const char *msg = "Unknown type encountered.";
Nan::ThrowTypeError(Nan::New<v8::String>(msg).ToLocalChecked());
return new Error(sass_make_error(msg));
}
}

Expand All @@ -61,7 +63,7 @@ namespace SassTypes
Value* Factory::unwrap(v8::Local<v8::Value> obj) {
// Todo: non-SassValue objects could easily fall under that condition, need to be more specific.
if (!obj->IsObject() || obj.As<v8::Object>()->InternalFieldCount() != 1) {
throw std::invalid_argument("A SassValue object was expected.");
return NULL;
}

return static_cast<Value*>(Nan::GetInternalFieldPointer(obj.As<v8::Object>(), 0));
Expand Down
14 changes: 9 additions & 5 deletions src/sass_types/list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@ namespace SassTypes
{
List::List(Sass_Value* v) : SassValueWrapper(v) {}

Sass_Value* List::construct(const std::vector<v8::Local<v8::Value>> raw_val) {
Sass_Value* List::construct(const std::vector<v8::Local<v8::Value>> raw_val, Sass_Value **out) {
size_t length = 0;
bool comma = true;

if (raw_val.size() >= 1) {
if (!raw_val[0]->IsNumber()) {
throw std::invalid_argument("First argument should be an integer.");
return fail("First argument should be an integer.", out);
}

length = Nan::To<uint32_t>(raw_val[0]).FromJust();

if (raw_val.size() >= 2) {
if (!raw_val[1]->IsBoolean()) {
throw std::invalid_argument("Second argument should be a boolean.");
return fail("Second argument should be a boolean.", out);
}

comma = Nan::To<bool>(raw_val[1]).FromJust();
}
}

return sass_make_list(length, comma ? SASS_COMMA : SASS_SPACE);
return *out = sass_make_list(length, comma ? SASS_COMMA : SASS_SPACE);
}

void List::initPrototype(v8::Local<v8::FunctionTemplate> proto) {
Expand Down Expand Up @@ -71,7 +71,11 @@ namespace SassTypes
}

Value* sass_value = Factory::unwrap(info[1]);
sass_list_set_value(unwrap(info.This())->value, Nan::To<uint32_t>(info[0]).FromJust(), sass_value->get_sass_value());
if (sass_value) {
sass_list_set_value(unwrap(info.This())->value, Nan::To<uint32_t>(info[0]).FromJust(), sass_value->get_sass_value());
} else {
Nan::ThrowTypeError(Nan::New<v8::String>("A SassValue is expected as the list item").ToLocalChecked());
}
}

NAN_METHOD(List::GetSeparator) {
Expand Down
2 changes: 1 addition & 1 deletion src/sass_types/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace SassTypes
public:
List(Sass_Value*);
static char const* get_constructor_name() { return "SassList"; }
static Sass_Value* construct(const std::vector<v8::Local<v8::Value>>);
static Sass_Value* construct(const std::vector<v8::Local<v8::Value>>, Sass_Value **);

static void initPrototype(v8::Local<v8::FunctionTemplate>);

Expand Down
18 changes: 13 additions & 5 deletions src/sass_types/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ namespace SassTypes
{
Map::Map(Sass_Value* v) : SassValueWrapper(v) {}

Sass_Value* Map::construct(const std::vector<v8::Local<v8::Value>> raw_val) {
Sass_Value* Map::construct(const std::vector<v8::Local<v8::Value>> raw_val, Sass_Value **out) {
size_t length = 0;

if (raw_val.size() >= 1) {
if (!raw_val[0]->IsNumber()) {
throw std::invalid_argument("First argument should be an integer.");
return fail("First argument should be an integer.", out);
}

length = Nan::To<uint32_t>(raw_val[0]).FromJust();
}

return sass_make_map(length);
return *out = sass_make_map(length);
}

void Map::initPrototype(v8::Local<v8::FunctionTemplate> proto) {
Expand Down Expand Up @@ -62,7 +62,11 @@ namespace SassTypes
}

Value* sass_value = Factory::unwrap(info[1]);
sass_map_set_value(unwrap(info.This())->value, Nan::To<uint32_t>(info[0]).FromJust(), sass_value->get_sass_value());
if (sass_value) {
sass_map_set_value(unwrap(info.This())->value, Nan::To<uint32_t>(info[0]).FromJust(), sass_value->get_sass_value());
} else {
Nan::ThrowTypeError(Nan::New<v8::String>("A SassValue is expected as a map value").ToLocalChecked());
}
}

NAN_METHOD(Map::GetKey) {
Expand Down Expand Up @@ -100,7 +104,11 @@ namespace SassTypes
}

Value* sass_value = Factory::unwrap(info[1]);
sass_map_set_key(unwrap(info.This())->value, Nan::To<uint32_t>(info[0]).FromJust(), sass_value->get_sass_value());
if (sass_value) {
sass_map_set_key(unwrap(info.This())->value, Nan::To<uint32_t>(info[0]).FromJust(), sass_value->get_sass_value());
} else {
Nan::ThrowTypeError(Nan::New<v8::String>("A SassValue is expected as a map key").ToLocalChecked());
}
}

NAN_METHOD(Map::GetLength) {
Expand Down
2 changes: 1 addition & 1 deletion src/sass_types/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace SassTypes
public:
Map(Sass_Value*);
static char const* get_constructor_name() { return "SassMap"; }
static Sass_Value* construct(const std::vector<v8::Local<v8::Value>>);
static Sass_Value* construct(const std::vector<v8::Local<v8::Value>>, Sass_Value **);

static void initPrototype(v8::Local<v8::FunctionTemplate>);

Expand Down
8 changes: 4 additions & 4 deletions src/sass_types/number.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@ namespace SassTypes
{
Number::Number(Sass_Value* v) : SassValueWrapper(v) {}

Sass_Value* Number::construct(const std::vector<v8::Local<v8::Value>> raw_val) {
Sass_Value* Number::construct(const std::vector<v8::Local<v8::Value>> raw_val, Sass_Value **out) {
double value = 0;
char const* unit = "";

if (raw_val.size() >= 1) {
if (!raw_val[0]->IsNumber()) {
throw std::invalid_argument("First argument should be a number.");
return fail("First argument should be a number.", out);
}

value = Nan::To<double>(raw_val[0]).FromJust();

if (raw_val.size() >= 2) {
if (!raw_val[1]->IsString()) {
throw std::invalid_argument("Second argument should be a string.");
return fail("Second argument should be a string.", out);
}

unit = create_string(raw_val[1]);
}
}

return sass_make_number(value, unit);
return *out = sass_make_number(value, unit);
}

void Number::initPrototype(v8::Local<v8::FunctionTemplate> proto) {
Expand Down
2 changes: 1 addition & 1 deletion src/sass_types/number.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace SassTypes
public:
Number(Sass_Value*);
static char const* get_constructor_name() { return "SassNumber"; }
static Sass_Value* construct(const std::vector<v8::Local<v8::Value>>);
static Sass_Value* construct(const std::vector<v8::Local<v8::Value>>, Sass_Value **out);

static void initPrototype(v8::Local<v8::FunctionTemplate>);

Expand Down
16 changes: 12 additions & 4 deletions src/sass_types/sass_value_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace SassTypes
static v8::Local<v8::Function> get_constructor();
static v8::Local<v8::FunctionTemplate> get_constructor_template();
static NAN_METHOD(New);
static Sass_Value *fail(const char *, Sass_Value **);

protected:
Sass_Value* value;
Expand Down Expand Up @@ -94,15 +95,15 @@ namespace SassTypes
localArgs[i] = info[i];
}
if (info.IsConstructCall()) {
try {
Sass_Value* value = T::construct(localArgs);
Sass_Value* value;
if (T::construct(localArgs, &value) != NULL) {
T* obj = new T(value);
sass_delete_value(value);

Nan::SetInternalFieldPointer(info.This(), 0, obj);
obj->js_object.Reset(info.This());
} catch (const std::exception& e) {
return Nan::ThrowError(Nan::New<v8::String>(e.what()).ToLocalChecked());
} else {
return Nan::ThrowError(Nan::New<v8::String>(sass_error_get_message(value)).ToLocalChecked());
}
} else {
v8::Local<v8::Function> cons = T::get_constructor();
Expand All @@ -117,8 +118,15 @@ namespace SassTypes

template <class T>
T* SassValueWrapper<T>::unwrap(v8::Local<v8::Object> obj) {
/* This maybe NULL */
return static_cast<T*>(Factory::unwrap(obj));
}

template <class T>
Sass_Value *SassValueWrapper<T>::fail(const char *reason, Sass_Value **out) {
*out = sass_make_error(reason);
return NULL;
}
}


Expand Down
Loading

0 comments on commit 43286a1

Please sign in to comment.