diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index d1a2f4a..a203f1e 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -63,3 +63,6 @@ jobs: run: | meson _build || cat _build/meson-logs/meson-log.txt ninja -C _build + - name: Run version + run: | + _build/version diff --git a/examples/version.cpp b/examples/version.cpp new file mode 100644 index 0000000..f89cd81 --- /dev/null +++ b/examples/version.cpp @@ -0,0 +1,54 @@ +#include +#include +#include + +#include +#include +#include + +#include "boilerplate.h" + +// Return JavaScript version +static bool version(JSContext* cx, unsigned argc, JS::Value* vp) { + JS::CallArgs args = JS::CallArgsFromVp(argc, vp); + const char* data = JS_GetImplementationVersion(); + JSString* str = JS_NewStringCopyZ(cx, data); + args.rval().setString(str); + return true; +} + +// Copy from hello.cpp +static bool ExecuteCodePrintResult(JSContext* cx, const char* code) { + JS::CompileOptions options(cx); + options.setFileAndLine("noname", 1); + + JS::SourceText source; + if (!source.init(cx, code, strlen(code), JS::SourceOwnership::Borrowed)) { + return false; + } + + JS::RootedValue rval(cx); + if (!JS::Evaluate(cx, options, source, &rval)) return false; + + printf("%s\n", JS_EncodeStringToASCII(cx, rval.toString()).get()); + return true; +} + +static bool RunVersion(JSContext* cx) { + JS::RootedObject global(cx, boilerplate::CreateGlobal(cx)); + if (!global) return false; + + JSAutoRealm ar(cx, global); + + // Define some helper methods on our new global. + if (!JS_DefineFunction(cx, global, "version", version, 0, 0)) return false; + + return ExecuteCodePrintResult(cx, R"js( + version() + )js"); +} + +int main(int argc, const char* argv[]) { + if (!boilerplate::RunExample(RunVersion)) return 1; + return 0; +} diff --git a/meson.build b/meson.build index aedfe63..8cba6c5 100644 --- a/meson.build +++ b/meson.build @@ -66,6 +66,7 @@ add_project_arguments(cxx.get_supported_arguments(test_warning_args), language: 'cpp') executable('hello', 'examples/hello.cpp', 'examples/boilerplate.cpp', dependencies: spidermonkey) +executable('version', 'examples/version.cpp', 'examples/boilerplate.cpp', dependencies: spidermonkey) executable('cookbook', 'examples/cookbook.cpp', 'examples/boilerplate.cpp', dependencies: spidermonkey) executable('repl', 'examples/repl.cpp', 'examples/boilerplate.cpp', dependencies: [spidermonkey, readline]) executable('tracing', 'examples/tracing.cpp', 'examples/boilerplate.cpp', dependencies: spidermonkey) diff --git a/tools/generic_lib.sh b/tools/generic_lib.sh index 7112f1a..0bad4f5 100755 --- a/tools/generic_lib.sh +++ b/tools/generic_lib.sh @@ -1,5 +1,5 @@ #!/bin/bash # Make SpiderMonkey and this repo generic instead of version specific. -# script shoud be run in mozjs dit with $1 for mason.build location +# script should be run in mozjs dir with $1 for mason.build location sed -i 's/mozjs-$MOZILLA_SYMBOLVERSION/mozjs/g' old-configure.in -sed -i --regexp-extended "s/dependency\(.mozjs.*$/dependency('mozjs')/gm" "$1" \ No newline at end of file +sed -i --regexp-extended "s/dependency\(.mozjs.*$/dependency('mozjs')/gm" "$1"