Skip to content

Commit

Permalink
core, feat: add full error type support (#381)
Browse files Browse the repository at this point in the history
  • Loading branch information
ngot authored and xicilion committed Nov 14, 2017
1 parent 89454b9 commit dbe3c74
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 18 deletions.
2 changes: 2 additions & 0 deletions fibjs/include/Isolate.h
Expand Up @@ -103,6 +103,8 @@ class Isolate : public exlib::linkitem {
v8::Global<v8::Context> m_context;
v8::Global<v8::Object> m_env;

v8::Global<v8::Object> m_AssertionError;

obj_ptr<LruCache> m_script_cache;
obj_ptr<SandBox> m_topSandbox;
obj_ptr<obj_base> m_httpclient;
Expand Down
108 changes: 90 additions & 18 deletions fibjs/include/utils.h
Expand Up @@ -998,59 +998,131 @@ inline v8::Local<v8::Object> GetIteratorReturnValue(v8::Isolate* isolate, v8::Lo
.ToLocalChecked();
}

inline v8::Local<v8::Value> ThrowError(const char* msg)
inline v8::Local<v8::Value> ThrowError(v8::Local<v8::Value> exception)
{
Isolate* isolate = Isolate::current();
return isolate->m_isolate->ThrowException(exception);
}

return isolate->m_isolate->ThrowException(v8::Exception::Error(
isolate->NewString(msg)));
inline v8::Local<v8::Value> ThrowError(result_t hr, exlib::string msg)
{
Isolate* isolate = Isolate::current();
v8::Local<v8::Value> exception = v8::Exception::Error(isolate->NewString(msg));
exception->ToObject()->Set(isolate->NewString("number"),
v8::Int32::New(isolate->m_isolate, -hr));
return ThrowError(exception);
}

inline v8::Local<v8::Value> ThrowError(v8::Local<v8::Value> exception)
inline v8::Local<v8::Value> ThrowError(const char* msg)
{
Isolate* isolate = Isolate::current();
return ThrowError(v8::Exception::Error(isolate->NewString(msg)));
}

return isolate->m_isolate->ThrowException(exception);
inline v8::Local<v8::Value> ThrowError(exlib::string msg)
{
return ThrowError(msg.c_str());
}

inline v8::Local<v8::Value> ThrowTypeError(const char* msg)
{
Isolate* isolate = Isolate::current();
return ThrowError(v8::Exception::TypeError(isolate->NewString(msg)));
}

return isolate->m_isolate->ThrowException(v8::Exception::TypeError(
isolate->NewString(msg)));
inline v8::Local<v8::Value> ThrowTypeError(exlib::string msg)
{
return ThrowTypeError(msg.c_str());
}

inline v8::Local<v8::Value> ThrowRangeError(const char* msg)
{
Isolate* isolate = Isolate::current();
return ThrowError(v8::Exception::RangeError(isolate->NewString(msg)));
}

return isolate->m_isolate->ThrowException(v8::Exception::RangeError(
isolate->NewString(msg)));
inline v8::Local<v8::Value> ThrowRangeError(exlib::string msg)
{
return ThrowRangeError(msg.c_str());
}

inline v8::Local<v8::Value> ThrowError(exlib::string msg)
inline v8::Local<v8::Value> ThrowReferenceError(const char* msg)
{
Isolate* isolate = Isolate::current();
return ThrowError(v8::Exception::ReferenceError(isolate->NewString(msg)));
}

return isolate->m_isolate->ThrowException(v8::Exception::Error(
isolate->NewString(msg)));
inline v8::Local<v8::Value> ThrowReferenceError(exlib::string msg)
{
return ThrowReferenceError(msg.c_str());
}

inline v8::Local<v8::Value> ThrowTypeError(exlib::string msg)
inline v8::Local<v8::Value> ThrowSyntaxError(const char* msg)
{
Isolate* isolate = Isolate::current();
return ThrowError(v8::Exception::SyntaxError(isolate->NewString(msg)));
}

return isolate->m_isolate->ThrowException(v8::Exception::TypeError(
isolate->NewString(msg)));
inline v8::Local<v8::Value> ThrowSyntaxError(exlib::string msg)
{
return ThrowSyntaxError(msg.c_str());
}

inline v8::Local<v8::Value> ThrowRangeError(exlib::string msg)
inline v8::Local<v8::Value> ThrowURIError(const char* msg)
{
Isolate* isolate = Isolate::current();
auto _context = isolate->context();
auto glob = _context->Global();
auto URIError = (glob->Get(isolate->NewString("URIError"))).As<v8::Object>();

v8::Local<v8::Value> args[] = { isolate->NewString(msg) };
auto error = URIError->CallAsConstructor(1, args);
return ThrowError(error);
}

inline v8::Local<v8::Value> ThrowURIError(exlib::string msg)
{
return ThrowURIError(msg.c_str());
}

inline v8::Local<v8::Value> ThrowEvalError(const char* msg)
{
Isolate* isolate = Isolate::current();
auto _context = isolate->context();
auto glob = _context->Global();
auto EvalError = (glob->Get(isolate->NewString("EvalError"))).As<v8::Object>();

v8::Local<v8::Value> args[] = { isolate->NewString(msg) };
auto error = EvalError->CallAsConstructor(1, args);
return ThrowError(error);
}

inline v8::Local<v8::Value> ThrowEvalError(exlib::string msg)
{
return ThrowEvalError(msg.c_str());
}

/**
* v8::Local<v8::Object> e = v8::Object::New(isolate->m_isolate);
* e->Set(isolate->NewString("actual"), isolate->NewString("actual msg"));
* e->Set(isolate->NewString("expected"), isolate->NewString("expected msg"));
* e->Set(isolate->NewString("message"), isolate->NewString("message msg"));
* e->Set(isolate->NewString("operator"), isolate->NewString("operator msg"));
* ThrowAssertionError(e);
**/
inline v8::Local<v8::Value> ThrowAssertionError(v8::Local<v8::Object> msg)
{
Isolate* isolate = Isolate::current();
v8::Local<v8::Value> args[] = { msg };
v8::Local<v8::Value> error;

{
v8::Local<v8::Object> AssertionError =
v8::Local<v8::Object>::New(isolate->m_isolate, isolate->m_AssertionError);
error = AssertionError->CallAsConstructor(1, args);
}

return isolate->m_isolate->ThrowException(v8::Exception::RangeError(
isolate->NewString(msg)));
return ThrowError(error);
}

inline result_t LastError()
Expand Down
28 changes: 28 additions & 0 deletions fibjs/src/base/Runtime.cpp
Expand Up @@ -176,6 +176,34 @@ void Isolate::init()

m_topSandbox = new SandBox();
m_topSandbox->initRoot();

auto assertion_error =
"class AssertionError extends Error {"
"constructor(options) {"
"var { actual, expected, message, operator } = options;"
"if (message) {"
"super(message);"
"} else {"
"if (actual && actual.stack && actual instanceof Error)"
"actual = `${actual.name}: ${actual.message}`;"
"if (expected && expected.stack && expected instanceof Error)"
"expected = `${expected.name}: ${expected.message}`;"
"super(`${JSON.stringify(actual).slice(0, 128)} ` +"
"`${operator} ${JSON.stringify(expected).slice(0, 128)}`);"
"}"
"this.generatedMessage = !message;"
"this.name = 'AssertionError [ERR_ASSERTION]';"
"this.code = 'ERR_ASSERTION';"
"this.actual = actual;"
"this.expected = expected;"
"this.operator = operator;"
"}"
"}"
"AssertionError;";

v8::Local<v8::Script> script = v8::Script::Compile(NewString(assertion_error));
v8::Local<v8::Object> AssertionError = script->Run().As<v8::Object>();
m_AssertionError.Reset(m_isolate, AssertionError);
}

void Isolate::start_profiler()
Expand Down

0 comments on commit dbe3c74

Please sign in to comment.