diff options
author | Chris Wilson <chris@chris-wilson.co.uk> | 2018-08-22 09:25:26 +0100 |
---|---|---|
committer | Chris Wilson <chris@chris-wilson.co.uk> | 2018-08-22 13:42:06 +0100 |
commit | 0b9ce172a7cb8c076893de66b23a83066ff98102 (patch) | |
tree | 11785ae18dc04da69c5efefcb6aa9419ed9f3877 | |
parent | ae6f9836ec6c7b0618abf65597e887041827b262 (diff) |
igt/shell: Minimal igt stub interface
-rw-r--r-- | shell/include/igt-shell.h | 3 | ||||
-rw-r--r-- | shell/lib/igt.cc | 98 | ||||
-rw-r--r-- | shell/meson.build | 1 |
3 files changed, 101 insertions, 1 deletions
diff --git a/shell/include/igt-shell.h b/shell/include/igt-shell.h index 59277d8e..2068690a 100644 --- a/shell/include/igt-shell.h +++ b/shell/include/igt-shell.h @@ -6,9 +6,10 @@ struct igt_builtin { struct igt_builtin *next; const char *name; - void (*ctor)(v8::Isolate *iso, v8::Handle<v8::ObjectTemplate> global); + void (*ctor)(v8::Isolate *iso, v8::Handle<v8::ObjectTemplate> tmpl); }; extern void igt_register_global(struct igt_builtin *tmpl); +extern void igt_register_builtin(struct igt_builtin *tmpl); #endif /* IGT_SHELL_H */ diff --git a/shell/lib/igt.cc b/shell/lib/igt.cc new file mode 100644 index 00000000..866f5467 --- /dev/null +++ b/shell/lib/igt.cc @@ -0,0 +1,98 @@ +#include "igt-shell.h" +#include "v8-helpers.h" + +using namespace v8; + +static void igt_require(const FunctionCallbackInfo<Value>& args) +{ + auto iso = args.GetIsolate(); + auto ctx = iso->GetCurrentContext(); + + if (!args[0]->BooleanValue(ctx).FromMaybe(0)) { + fprintf(stderr, "Requirement failed!"); + iso->ThrowException(Exception::Error(AsString(iso, "bad requirement").ToLocalChecked())); + return; + } +} + +static void igt_assert(const FunctionCallbackInfo<Value>& args) +{ + auto iso = args.GetIsolate(); + auto ctx = iso->GetCurrentContext(); + + if (!args[0]->BooleanValue(ctx).FromMaybe(0)) { + fprintf(stderr, "Assertion failed!"); + iso->ThrowException(Exception::Error(AsString(iso, "Assertion failed").ToLocalChecked())); + return; + } +} + +static void igt_expect(const FunctionCallbackInfo<Value>& args) +{ + auto iso = args.GetIsolate(); + + if (!args[0]->IsFunction() || !args[1]->IsInt32()) { + iso->ThrowException(Exception::TypeError(AsString(iso, "bad args").ToLocalChecked())); + return; + } + int expect = args[1]->Int32Value(); + + TryCatch try_catch(iso); + + auto ctx = iso->GetCurrentContext(); + Local<Function> fn = Local<Function>::Cast(args[0]->ToObject()); + + Local<Value> result; + if (fn->Call(ctx, fn, 0, NULL).ToLocal(&result)) { + if (result->Int32Value(ctx).FromMaybe(0) != expect) + iso->ThrowException(Exception::Error(AsString(iso, "function returned unexpected result").ToLocalChecked())); + return; + } + + if (!try_catch.HasCaught()) { + iso->ThrowException(Exception::Error(AsString(iso, "function succeed, expecting failure").ToLocalChecked())); + return; + } + + // extract errno from try_catch +} + +static struct igt_builtin *modules; + +static void igt_global_ctor(Isolate *iso, Handle<ObjectTemplate> global) +{ + HandleScope handle_scope(iso); + + auto tmpl = ObjectTemplate::New(iso); + + static const struct v8_helper_fn funcs[] = { + { "require", igt_require }, + { "assert", igt_assert }, + { "expect", igt_expect }, + + {} + }; + v8_helper_funcs(iso, tmpl, funcs); + + for (auto *b = modules; b; b = b->next) + b->ctor(iso, tmpl); + + global->Set(iso, "igt", tmpl); +} + +void igt_register_builtin(struct igt_builtin *b) +{ + b->next = modules; + modules = b; +} + +static struct igt_builtin igt_global = { + .name = "igt", + .ctor = igt_global_ctor +}; + +__attribute__((constructor)) +static void __igt_register_global__(void) +{ + igt_register_global(&igt_global); +} diff --git a/shell/meson.build b/shell/meson.build index 87aaa2e9..ff4d870f 100644 --- a/shell/meson.build +++ b/shell/meson.build @@ -1,5 +1,6 @@ executable('igt', [ 'igt-shell.cc', + 'lib/igt.cc', 'lib/v8-helpers.cc', ], objects: 'v8/libv8_monolith.a', |