-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathhello.cpp
More file actions
59 lines (48 loc) · 1.8 KB
/
Copy pathhello.cpp
File metadata and controls
59 lines (48 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <cstdio>
#include <jsapi.h>
#include <js/CompilationAndEvaluation.h>
#include <js/SourceText.h>
#include "boilerplate.h"
// This example illustrates the bare minimum you need to do to execute a
// JavaScript program using embedded SpiderMonkey. It does no error handling and
// simply exits if something goes wrong.
//
// See 'boilerplate.cpp' for the parts of this example that are reused in many
// simple embedding examples.
//
// To use the interpreter you need to create a context and a global object, and
// do some setup on both of these. You also need to enter a "realm" (environment
// within one global object) before you can execute code.
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;
// There are many ways to display an arbitrary value as a result. In this
// case, we know that the value is an ASCII string because of the expression
// that we executed, so we can just print the string directly.
printf("%s\n", JS_EncodeStringToASCII(cx, rval.toString()).get());
return true;
}
static bool HelloExample(JSContext* cx) {
JS::RootedObject global(cx, boilerplate::CreateGlobal(cx));
if (!global) {
return false;
}
JSAutoRealm ar(cx, global);
// The 'js' delimiter is meaningless, but it's useful for marking C++ raw
// strings semantically.
return ExecuteCodePrintResult(cx, R"js(
`hello world, it is ${new Date()}`
)js");
}
int main(int argc, const char* argv[]) {
if (!boilerplate::RunExample(HelloExample)) {
return 1;
}
return 0;
}