Oolite
Loading...
Searching...
No Matches
main.m
Go to the documentation of this file.
1/*
2
3main.m
4
5Test driver for the QuickJS-ng proof of concept (issue #580). Loads scripts
6from the tests directory given on the command line, checks their behaviour
7and the engine's error reporting, then destroys the runtime; debug builds of
8QuickJS abort if any JS object has leaked.
9
10Usage: oojs-quickjs-poc <tests directory>
11
12*/
13
14#import "OOJSScript.h"
15#import "OOJavaScriptEngine.h"
16#import "legacy_random.h"
17
18#include <time.h>
19
20NSArray* OOPocRecordedLogMessages(void);
22
23static unsigned sFailures = 0;
24static unsigned sChecks = 0;
25
26static void Check(BOOL condition, NSString* description)
27{
28 sChecks++;
29 if (!condition) {
30 sFailures++;
31 fprintf(stdout, "FAIL: %s\n", [description UTF8String]);
32 } else {
33 fprintf(stdout, "ok: %s\n", [description UTF8String]);
34 }
35 fflush(stdout);
36}
37
38static BOOL LogContains(NSString* text)
39{
40 for (NSString* line in OOPocRecordedLogMessages()) {
41 if ([line rangeOfString:text].location != NSNotFound)
42 return YES;
43 }
44 return NO;
45}
46
47static double Now(void)
48{
49 struct timespec now;
50 clock_gettime(CLOCK_MONOTONIC, &now);
51 return now.tv_sec + now.tv_nsec * 1e-9;
52}
53
54// Call a script method and convert its result to a string; nil if the call failed.
55static NSString* CallForString(OOJSScript* script, NSString* method)
56{
57 JSContext* context = OOJSAcquireContext();
58 JSValue result;
59 NSString* string = nil;
60
61 if ([script callMethodNamed:method withArguments:NULL count:0 result:&result]) {
62 string = OOStringFromJSValueEvenIfNull(context, result);
63 JS_FreeValue(context, result);
64 }
65 return string;
66}
67
68static void RunBehaviourTests(NSString* testDir, NSString* fileName)
69{
70 OOJSScript* script = [OOJSScript scriptWithPath:[testDir stringByAppendingPathComponent:fileName]];
71 Check(script != nil, [NSString stringWithFormat:@"%@ loads", fileName]);
72
73 NSString* failures = CallForString(script, @"runTests");
74 Check([failures isEqualToString:@"0"], [NSString stringWithFormat:@"%@ behaviour tests pass (failures: %@)", fileName, failures]);
75}
76
77static void RunScopeTests(NSString* testDir)
78{
79 OOJSScript* scriptA = [OOJSScript scriptWithPath:[testDir stringByAppendingPathComponent:@"scope_a.js"]];
80 OOJSScript* scriptB = [OOJSScript scriptWithPath:[testDir stringByAppendingPathComponent:@"scope_b.js"]];
81 Check(scriptA != nil && scriptB != nil, @"scripts with the same top-level const and var names both load");
82 Check([[scriptA displayName] isEqualToString:@"poc_scope_a 1.2"], @"script name and version read after initial run");
83
84 NSString* result = CallForString(scriptA, @"startUp");
85 NSString* expected = @"1|ab..|poc_scope_a|1.2|a|a|undefined|late|strict|poc_scope_a";
86 Check([result isEqualToString:expected], [NSString stringWithFormat:@"bare names resolve through the script object, strict mode kept (got %@)", result]);
87
88 result = CallForString(scriptB, @"check");
89 Check([result isEqualToString:@"bbpoc_scope_b"], [NSString stringWithFormat:@"second script sees its own top-level declarations (got %@)", result]);
90}
91
92static void RunErrorTests(NSString* testDir)
93{
95 OOJSScript* script = [OOJSScript scriptWithPath:[testDir stringByAppendingPathComponent:@"runtime_error.js"]];
96 Check(CallForString(script, @"startUp") == nil, @"handler that throws reports failure");
97 Check(LogContains(@"[script.javaScript.exception.ReferenceError]: ***** JavaScript exception (poc_runtime_error): ReferenceError: undefinedFunction is not defined"), @"runtime exception logged with class, script name and message");
98 Check(LogContains(@"runtime_error.js, line 9."), @"runtime exception logged with correct file and line");
99
101 script = [OOJSScript scriptWithPath:[testDir stringByAppendingPathComponent:@"syntax_error.js"]];
102 Check(script == nil, @"script with syntax error fails to load");
103 Check(LogContains(@"[script.javaScript.exception.SyntaxError]"), @"syntax error logged");
104 Check(LogContains(@"syntax_error.js, line "), @"syntax error logged with file and line");
105}
106
107static void RunTimeLimiterTests(NSString* testDir)
108{
110 OOJSScript* script = [OOJSScript scriptWithPath:[testDir stringByAppendingPathComponent:@"runaway.js"]];
111
112 double start = Now();
113 NSString* result = CallForString(script, @"startUp");
114 double elapsed = Now() - start;
115
116 Check(result == nil, @"infinite loop is terminated");
117 Check(elapsed >= 1.0 && elapsed < 3.0, [NSString stringWithFormat:@"termination after the 1 second limit (took %.2f s)", elapsed]);
118 Check(LogContains(@"Script \"poc_runaway\" ran for"), @"termination logged");
119 Check(!LogContains(@"JavaScript exception"), @"termination not also reported as an exception");
120 Check([CallForString(script, @"ping") isEqualToString:@"pong"], @"engine still usable after termination");
121}
122
123int main(int argc, const char* argv[])
124{
125 if (argc < 2) {
126 fprintf(stderr, "Usage: %s <tests directory>\n", argv[0]);
127 return 2;
128 }
129
130 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
131 NSString* testDir = [NSString stringWithUTF8String:argv[1]];
132
133 // As the game does at startup; an unseeded RANROT returns 0 forever.
134 ranrot_srand((uint32_t)time(NULL));
136
137 // Each group gets its own pool so all scripts are released before shutdown.
138 NSAutoreleasePool* testPool = [[NSAutoreleasePool alloc] init];
139 RunBehaviourTests(testDir, @"vector.js");
140 RunBehaviourTests(testDir, @"quaternion.js");
141 RunScopeTests(testDir);
142 RunErrorTests(testDir);
143 RunTimeLimiterTests(testDir);
144 [testPool release];
145
146 NSUInteger leaked = [engine shutDown];
147 Check(leaked == 0, [NSString stringWithFormat:@"runtime destroyed with no leaked objects (%lu alive)", (unsigned long)leaked]);
148
149 fprintf(stdout, "\n%u checks, %u failures\n", sChecks, sFailures);
150 [pool release];
151 return sFailures == 0 ? 0 : 1;
152}
void OOPocClearRecordedLogMessages(void)
Definition OOLogStub.m:20
static void RunTimeLimiterTests(NSString *testDir)
Definition main.m:107
static NSString * CallForString(OOJSScript *script, NSString *method)
Definition main.m:55
static void Check(BOOL condition, NSString *description)
Definition main.m:26
static BOOL LogContains(NSString *text)
Definition main.m:38
NSArray * OOPocRecordedLogMessages(void)
Definition OOLogStub.m:15
static unsigned sChecks
Definition main.m:24
static double Now(void)
Definition main.m:47
static unsigned sFailures
Definition main.m:23
int main(int argc, const char *argv[])
Definition main.m:123
static void RunScopeTests(NSString *testDir)
Definition main.m:77
static void RunBehaviourTests(NSString *testDir, NSString *fileName)
Definition main.m:68
static void RunErrorTests(NSString *testDir)
Definition main.m:92
OOINLINE JSContext * OOJSAcquireContext(void)
NSString * OOStringFromJSValueEvenIfNull(JSContext *context, jsval value)
unsigned count
return nil
id scriptWithPath:(NSString *path)
Definition OOJSScript.m:50
OOJavaScriptEngine * sharedEngine()
void ranrot_srand(uint32_t seed)