Skip to content

Commit 123ae6a

Browse files
author
Karl Malbrain
committed
Clean up jsscripts, add -Debug option.
1 parent cd4e46c commit 123ae6a

29 files changed

+68
-65
lines changed

js.h

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
#define strcasecmp _strnicmp
1313
#endif
1414

15+
extern bool MathNums;
16+
extern bool debug;
17+
1518
// memory allocation
1619

1720
void *js_realloc(void *old, uint32_t size, bool zeroit);

js_db.c

-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
#include "database/db_api.h"
44
#include "database/db_error.h"
55

6-
static bool debug = false;
7-
86
// types of handles/arenas
97

108
extern void marshal_doc(value_t document, uint8_t *doc, uint32_t docSize);

js_dbfind.c

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include "js.h"
22
#include "js_eval.h"
33

4-
bool debug = false;
54
bool init = true;
65

76
typedef bool (*queryFcn)(value_t r, value_t tst);

js_dbinsert.c

-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
#include "database/db.h"
33
#include "database/db_api.h"
44

5-
static bool debug = false;
6-
75
extern uint32_t calcSize (value_t doc);
86
extern void marshal_doc(value_t document, uint8_t *doc, uint32_t docSize);
97

js_error.c

+10-5
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@
22

33
char *strstatus(Status s) {
44
switch (s) {
5-
case OK: return "OK";
6-
case ERROR_outofmemory: return "out of memory";
7-
case ERROR_script_internal: return "script internal error";
5+
case OK: return "OK";
6+
case ERROR_outofmemory: return "out of memory";
7+
case ERROR_script_internal: return "script internal error";
88
case ERROR_script_unrecognized_function: return "script unrecognized function";
9-
case ERROR_tcperror: return "tcperror";
10-
case ERROR_bsonformat: return "bsonformat";
9+
case ERROR_tcperror: return "tcperror";
10+
case ERROR_bsonformat: return "bsonformat";
1111
case ERROR_notobject_or_array: return "not object or array";
12+
case ERROR_mathdomain: return "outside math domain";
13+
case ERROR_endoffile: return "end of file";
14+
case ERROR_doesnot_exist: return "does not exist";
15+
case ERROR_script_parse: return "script parse";
16+
case ERROR_json_parse: return "json parse";
1217
default:;
1318
}
1419
return NULL;

js_eval.c

-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
#include "js.h"
22
#include "js_malloc.h"
33

4-
static bool debug = false;
5-
extern bool MathNums;
6-
74
int ArraySize[] = {
85
sizeof(value_t),
96
sizeof(int8_t),

js_fcns.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,8 @@ void execScripts(Node *table, uint32_t size, value_t args, symtab_t *symbols, en
259259
dispatch(fn->begin, env);
260260

261261
elapsed = getCpuTime(0) - strtTime;
262-
fprintf (stderr, "Execution: %dm%.6fs %s \n", (int)(elapsed/60), elapsed - (int)(elapsed/60)*60, fn->script);
262+
if (debug)
263+
fprintf (stderr, "Execution: %dm%.6fs %s \n", (int)(elapsed/60), elapsed - (int)(elapsed/60)*60, fn->script);
263264
}
264265

265266
v.bits = vt_closure;

js_lib.c

+4-6
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
1919
#define strerror_s(buf,siz,err) (strerror_r(err,buf,siz))
2020
#endif
2121

22-
static bool debug = false;
23-
extern bool MathNums;
24-
2522
value_t js_setOption(uint32_t args, environment_t *env) {
2623
value_t v, s;
2724

@@ -36,6 +33,9 @@ value_t js_setOption(uint32_t args, environment_t *env) {
3633
return s.status = ERROR_script_internal, s;
3734
}
3835

36+
if (!memcmp(v.str, "Debug", 6))
37+
debug = true;
38+
3939
if (!memcmp(v.str, "Math", 5))
4040
MathNums = true;
4141

@@ -366,9 +366,7 @@ value_t js_listFiles(uint32_t args, environment_t *env) {
366366
HANDLE hndl;
367367

368368
memcpy (pattern, path.str, path.aux > MAX_PATH - 2 ? MAX_PATH - 2 : path.aux);
369-
pattern[path.aux] = '/';
370-
pattern[path.aux+1] = '*';
371-
pattern[path.aux+2] = 0;
369+
pattern[path.aux] = 0;
372370

373371
hndl = FindFirstFile(pattern, fd);
374372

js_main.c

+12-6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#endif
1414

1515
bool MathNums; // interpret numbers as doubles
16+
bool debug;
1617

1718
void memInit(void);
1819

@@ -74,11 +75,6 @@ int main(int argc, char* argv[]) {
7475

7576
memInit();
7677

77-
printf("sizeof value_t = %d\n", (int)sizeof(value_t));
78-
printf("sizeof Node = %d\n", (int)sizeof(Node));
79-
printf("sizeof Object = %d\n", (int)sizeof(object_t));
80-
printf("sizeof raw_t = %d\n", (int)sizeof(rawobj_t));
81-
8278
for (int i = 0; i < node_MAX; i++)
8379
dispatchTable[i] = eval_badop;
8480

@@ -121,6 +117,8 @@ int main(int argc, char* argv[]) {
121117
while (--argc > 0 && (++argv)[0][0] == '-') {
122118
if (!strcmp(argv[0], "-Math"))
123119
MathNums = true;
120+
if (!strcmp(argv[0], "-Debug"))
121+
debug = true;
124122
else if(!strcmp(argv[0], "-Write") && argc > 1) {
125123
if((err = fopen_s(&strm, argv[1], "wb"))) {
126124
strerror_s(errmsg, sizeof(errmsg), err);
@@ -150,7 +148,8 @@ int main(int argc, char* argv[]) {
150148
strerror_s(errmsg, sizeof(errmsg), err);
151149
fprintf(stderr, "Error: unable to open '%s' error: %d: %s\n", argv[nScripts], err, errmsg);
152150
} else {
153-
fprintf(stderr, "Compiling: %s\n", argv[nScripts]);
151+
if (debug)
152+
fprintf(stderr, "Compiling: %s\n", argv[nScripts]);
154153
pd->script = argv[nScripts];
155154
loadScript(pd);
156155
fclose(dummy);
@@ -174,6 +173,13 @@ int main(int argc, char* argv[]) {
174173

175174
execScripts(pd->table, pd->tableNext, args, globalSymbols, NULL);
176175

176+
if (debug) {
177+
printf("sizeof value_t = %d\n", (int)sizeof(value_t));
178+
printf("sizeof Node = %d\n", (int)sizeof(Node));
179+
printf("sizeof Object = %d\n", (int)sizeof(object_t));
180+
printf("sizeof raw_t = %d\n", (int)sizeof(rawobj_t));
181+
}
182+
177183
// TODO: delete objects in the global frame
178184

179185
return 0;

js_malloc.c

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
#include "js_malloc.h"
99
#include "database/db_malloc.h"
1010

11-
static bool debug = true;
12-
1311
// allocate reference counted object
1412

1513
uint64_t js_rawAlloc(uint32_t size, bool zeroit) {

js_math.c

-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
#include "js_math.h"
1212
#include "js_malloc.h"
1313

14-
static int debug = 0;
15-
1614
value_t conv(value_t val, valuetype_t type, bool abandon) {
1715
value_t result;
1816

js_symbols.c

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#include "js.h"
22

3-
static bool debug = false;
4-
53
extern int builtin (stringNode *name);
64
extern symtab_t globalSymbols[1];
75

jsscripts/test_fcn.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ function x(y,z){
66
}
77
}
88

9+
print("expecting 1");
910
x("yes1", true);
1011

1112
function a(y,z){
@@ -16,6 +17,6 @@ function a(y,z){
1617
}
1718
}
1819

19-
print(x("yes1", true));
20-
print(a("yes2", true));
20+
print("Expecting yes2true:", x("yes1", true));
21+
print("Expecting undefined:", a("yes2", true));
2122

jsscripts/test_fcn2.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ function bottomUpTree(item,depth){
2323
}
2424
}
2525

26-
print(bottomUpTree(0,0).itemCheck());
26+
print("Expecting 0: ", bottomUpTree(0,0).itemCheck());

jsscripts/test_for.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
var array = [3,2,1];
22

3+
print("array: ", array);
4+
35
for( var idx in array)
4-
print("idx: ", idx);
6+
print("idx values in array: ", idx);
57

68
for( var val of array)
7-
print("val: ", val);
9+
print("val values of array: ", val);

jsscripts/test_ifthenelse.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
var a = 1, b = 2;
22

3+
print("expecting 2:");
4+
35
if (a) {
46
if (b) print(b);
57
} else

jsscripts/test_int.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
var i = 2.0;
2-
print(i);
2+
print("Expecting 2.0: ", i);
File renamed without changes.

jsscripts/test_math.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
var v = [1.2,2.2,3.2];
2-
print(v[0], v[1], v[2]);
3-
print(v[0] * v[1] * v[2]);
2+
print("Vector :", v);
3+
4+
print("Multiplied: ", v[0] * v[1] * v[2]);
45

56
var a = 1;
67

78
for(var i = 0; i < v.length; i++)
89
a /= v[i];
910

10-
print(i, ": vec div = ", a);
11+
print("1/", v[0] * v[1] * v[2], " = ", a);

jsscripts/test_name.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11

22
function Test (a,b,c) {
3-
print(Test.displayName);
43
return { test : a, test2:b, test3:c, name : Test.displayName };
54
}
65

7-
print(Test(1,2,3));
6+
print("Call Test(1,2,3), expecting { test : a, test2:b, test3:c, name : Test.displayName }: ", Test(1,2,3));

jsscripts/test_obj.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
var doc = { field: 1,};
2-
print(doc);
2+
print("Expecting { field: 1,} : ", doc);
33

44
doc = [1,2];
5-
print(doc);
5+
print("Expecting [1,2] : ", doc);
66

77
function NewObj (a,b,c) {
88
this.sum = a+b+c;
99
}
1010

11-
print("begin test");
1211
NewObj.prototype.type = "NewObj";
1312

1413
var test = new NewObj(1,2,3);
1514

16-
print(test.type);
15+
print("Expecting NewObj: ", test.type);
1716

1817

jsscripts/test_parse.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
print(parseInt("1"));
2-
print(parseFloat("1.2"));
1+
print('parseInt("1") = ', parseInt("1"));
2+
print('parseFloat("1.2") = ', parseFloat("1.2"));
33

4-
print(parseInt("a"));
5-
print(parseFloat("a"));
4+
print('parseInt("a") = ', parseInt("a"));
5+
print('parseFloat("a") = ', parseFloat("a"));

jsscripts/test_proto.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var o = {
55
}
66
};
77

8-
print(o.f()); // logs 37
8+
print("Expecting 37: ", o.f());
99

1010
Function.prototype.toString = function() {
1111
return "Fcn: " + this.name;

jsscripts/test_rel.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
print("a" > "b");
2-
print("a" < "b");
3-
print("aa" < "aa");
4-
print("aa" <= "aa");
5-
print("aa" <= "aab");
6-
print("aa" > "aab");
1+
print('"a" > "b" = ', "a" > "b");
2+
print('"a" < "b" = ', "a" < "b");
3+
print('"aa" < "aa" = ', "aa" < "aa");
4+
print('"aa" <= "aa" = ', "aa" <= "aa");
5+
print('"aa" <= "aab" = ', "aa" <= "aab");
6+
print('"aa" > "aab" = ', "aa" > "aab");

jsscripts/test_semi.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
var x = 2 * 3.0;
2-
print(x);;
3-
print("ok");
2+
print("Expecting: 6.0: ", x);;
3+
print("Empty Statement Ignored");

jsscripts/test_sort.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
var source = [9,3,8,1];
2-
print(source.sort());
2+
print("Sorting: ", source, " result: ", source.sort());

jsscripts/test_str.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ var array = ["aaaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbb", "cccccccccccccccc", 4];
4646

4747
print ('\nmake array["aaaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbb", "cccccccccccccccc", 4]: ', array);
4848
print("array[0].charCodeAt(6): ", array[0].charCodeAt(6));
49-
print(typeof array[0]);
49+
print("Expecting typeof array[0]: ", typeof array[0]);
5050

5151
print("\narray.join w/'->': ", array.join("->"));
5252
print("\nmake 16 byte chunks\n");

jsscripts/test_str1.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
var array = ["a", 2, 3, "d"];
22

3-
print(array);
3+
print('Expecting ["a", 2, 3, "d"] : ', array);
44

55
var str = " ";
66

77
for(var i = 0; i < 1024 * 1024; i++)
88
str += " ";
99

10-
print(str.length);
10+
print("Expecting 1048577: ", str.length);
1111

jsscripts/test_tern.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
var x = 1>2 ? "good" : 1<2 ? "good2" : "bad";
2-
print(x);
2+
print("expecting good2: ", x);

0 commit comments

Comments
 (0)