Source code

Revision control

Other Tools

1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
* License, v. 2.0. If a copy of the MPL was not distributed with this
3
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
#include <stdio.h>
6
#include <stdlib.h>
7
#include <string.h>
8
9
#include "gdb-tests.h"
10
#include "jsapi.h"
11
#include "jsfriendapi.h"
12
#include "js/Initialization.h"
13
#include "js/Warnings.h" // JS::SetWarningReporter
14
15
using namespace JS;
16
17
/* The class of the global object. */
18
static const JSClass global_class = {"global", JSCLASS_GLOBAL_FLAGS,
19
&DefaultGlobalClassOps};
20
21
static volatile int dontOptimizeMeAway = 0;
22
23
void usePointer(const void* ptr) { dontOptimizeMeAway++; }
24
25
template <typename T>
26
static inline T* checkPtr(T* ptr) {
27
if (!ptr) {
28
abort();
29
}
30
return ptr;
31
}
32
33
static void checkBool(bool success) {
34
if (!success) {
35
abort();
36
}
37
}
38
39
/* The warning reporter callback. */
40
void reportWarning(JSContext* cx, JSErrorReport* report) {
41
fprintf(stderr, "%s:%u: %s\n",
42
report->filename ? report->filename : "<no filename>",
43
(unsigned int)report->lineno, report->message().c_str());
44
}
45
46
// prologue.py sets a breakpoint on this function; test functions can call it
47
// to easily return control to GDB where desired.
48
void breakpoint() {
49
// If we leave this function empty, the linker will unify it with other
50
// empty functions throughout SpiderMonkey. If we then set a GDB
51
// breakpoint on it, that breakpoint will hit at all sorts of random
52
// times. So make it perform a distinctive side effect.
53
fprintf(stderr, "Called " __FILE__ ":breakpoint\n");
54
}
55
56
GDBFragment* GDBFragment::allFragments = nullptr;
57
58
int main(int argc, const char** argv) {
59
if (!JS_Init()) return 1;
60
JSContext* cx = checkPtr(JS_NewContext(1024 * 1024));
61
62
JS_SetGCParameter(cx, JSGC_MAX_BYTES, 0xffffffff);
63
JS_SetNativeStackQuota(cx, 5000000);
64
65
checkBool(JS::InitSelfHostedCode(cx));
66
JS::SetWarningReporter(cx, reportWarning);
67
68
/* Create the global object. */
69
JS::RealmOptions options;
70
RootedObject global(
71
cx, checkPtr(JS_NewGlobalObject(cx, &global_class, nullptr,
72
JS::FireOnNewGlobalHook, options)));
73
JSAutoRealm ar(cx, global);
74
75
argv++;
76
while (*argv) {
77
const char* name = *argv++;
78
GDBFragment* fragment;
79
for (fragment = GDBFragment::allFragments; fragment;
80
fragment = fragment->next) {
81
if (strcmp(fragment->name(), name) == 0) {
82
fragment->run(cx, argv);
83
break;
84
}
85
}
86
if (!fragment) {
87
fprintf(stderr, "Unrecognized fragment name: %s\n", name);
88
exit(1);
89
}
90
}
91
92
return 0;
93
}