Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,6 @@ jobs:
run: |
meson _build || cat _build/meson-logs/meson-log.txt
ninja -C _build
- name: Run version
run: |
_build/version
54 changes: 54 additions & 0 deletions examples/version.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <cassert>
#include <iostream>
#include <cstdio>

#include <jsapi.h>
#include <js/CompilationAndEvaluation.h>
#include <js/SourceText.h>

#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<mozilla::Utf8Unit> 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;
}
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions tools/generic_lib.sh
Original file line number Diff line number Diff line change
@@ -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"
sed -i --regexp-extended "s/dependency\(.mozjs.*$/dependency('mozjs')/gm" "$1"