From c8f45ad64f1366b4b30d89cb650d04e0f7eb8d2a Mon Sep 17 00:00:00 2001 From: sagu <16504129+sagudev@users.noreply.github.com> Date: Thu, 17 Jun 2021 19:25:25 +0200 Subject: [PATCH 1/3] Update generic_lib.sh --- tools/generic_lib.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/generic_lib.sh b/tools/generic_lib.sh index 7112f1a..e4ede1c 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 shoud 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" From 3022bf00f52ffc3fbc4351eee412fdba375ec904 Mon Sep 17 00:00:00 2001 From: sagudev Date: Thu, 17 Jun 2021 19:28:08 +0200 Subject: [PATCH 2/3] Fix typo --- tools/generic_lib.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/generic_lib.sh b/tools/generic_lib.sh index e4ede1c..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 dir 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" From 102e81936eacfad5f92e02ffd7f916cdcf6e6a3f Mon Sep 17 00:00:00 2001 From: sagudev Date: Fri, 18 Jun 2021 06:19:05 +0200 Subject: [PATCH 3/3] Add version example --- .github/workflows/build.yml | 3 +++ examples/version.cpp | 54 +++++++++++++++++++++++++++++++++++++ meson.build | 1 + 3 files changed, 58 insertions(+) create mode 100644 examples/version.cpp 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)