Skip to content

Commit 40a12fb

Browse files
committed
Split header into js.h public and jsi.h private. Start cleaning up
private function prefixes.
1 parent 8aa0627 commit 40a12fb

33 files changed

+437
-416
lines changed

js.h

+72-65
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,93 @@
11
#ifndef js_h
22
#define js_h
33

4-
#include <stdio.h>
5-
#include <stdlib.h>
6-
#include <stddef.h>
7-
#include <stdarg.h>
8-
#include <string.h>
9-
#include <setjmp.h>
10-
#include <math.h>
11-
#include <float.h>
12-
13-
/* noreturn is a GCC extension */
14-
#ifdef __GNUC__
15-
#define JS_NORETURN __attribute__((noreturn))
16-
#else
17-
#ifdef _MSC_VER
18-
#define JS_NORETURN __declspec(noreturn)
19-
#else
20-
#define JS_NORETURN
21-
#endif
22-
#endif
23-
24-
/* GCC can do type checking of printf strings */
25-
#ifndef __printflike
26-
#if __GNUC__ > 2 || __GNUC__ == 2 && __GNUC_MINOR__ >= 7
27-
#define __printflike(fmtarg, firstvararg) \
28-
__attribute__((__format__ (__printf__, fmtarg, firstvararg)))
29-
#else
30-
#define __printflike(fmtarg, firstvararg)
31-
#endif
32-
#endif
4+
#include "jsconf.h"
335

346
typedef struct js_State js_State;
7+
typedef int (*js_CFunction)(js_State *J, int argc);
358

36-
#define JS_REGEXP_G 1
37-
#define JS_REGEXP_I 2
38-
#define JS_REGEXP_M 4
9+
/* Basic functions */
3910

4011
js_State *js_newstate(void);
41-
void js_close(js_State *J);
12+
void js_freestate(js_State *J);
4213

43-
void js_loadstring(js_State *J, const char *source);
44-
void js_loadfile(js_State *J, const char *filename);
4514
int js_dostring(js_State *J, const char *source);
4615
int js_dofile(js_State *J, const char *filename);
4716

4817
void js_gc(js_State *J, int report);
4918

50-
/* binding API: TODO: move from jsrun.h */
19+
const char *js_intern(js_State *J, const char *s);
5120

52-
typedef int (*js_CFunction)(js_State *J, int argc);
21+
/* Push a new Error object with the formatted message and throw it */
22+
JS_NORETURN void js_error(js_State *J, const char *fmt, ...) JS_PRINTFLIKE(2,3);
5323

54-
/* private */
24+
/* Property attribute flags */
25+
enum {
26+
JS_READONLY = 1,
27+
JS_DONTENUM = 2,
28+
JS_DONTDELETE = 4,
29+
};
5530

56-
typedef struct js_Ast js_Ast;
57-
typedef struct js_Environment js_Environment;
58-
typedef struct js_Function js_Function;
59-
typedef struct js_Object js_Object;
60-
typedef struct js_StringNode js_StringNode;
31+
JS_NORETURN void js_throw(js_State *J);
6132

62-
const char *js_intern(js_State *J, const char *s);
63-
void js_printstrings(js_State *J);
64-
void js_freestrings(js_State *J);
65-
66-
void jsB_initobject(js_State *J);
67-
void jsB_initarray(js_State *J);
68-
void jsB_initfunction(js_State *J);
69-
void jsB_initboolean(js_State *J);
70-
void jsB_initnumber(js_State *J);
71-
void jsB_initstring(js_State *J);
72-
void jsB_initerror(js_State *J);
73-
void jsB_initmath(js_State *J);
33+
void js_loadstring(js_State *J, const char *filename, const char *source);
34+
void js_loadfile(js_State *J, const char *filename);
7435

75-
JS_NORETURN void js_throw(js_State *J);
76-
JS_NORETURN void js_error(js_State *J, const char *fmt, ...) __printflike(2,3);
77-
78-
JS_NORETURN void jsR_throwError(js_State *J, const char *message);
79-
JS_NORETURN void jsR_throwEvalError(js_State *J, const char *message);
80-
JS_NORETURN void jsR_throwRangeError(js_State *J, const char *message);
81-
JS_NORETURN void jsR_throwReferenceError(js_State *J, const char *message);
82-
JS_NORETURN void jsR_throwSyntaxError(js_State *J, const char *message);
83-
JS_NORETURN void jsR_throwTypeError(js_State *J, const char *message);
84-
JS_NORETURN void jsR_throwURIError(js_State *J, const char *message);
36+
void js_call(js_State *J, int n);
37+
void js_construct(js_State *J, int n);
38+
39+
void js_getglobal(js_State *J, const char *name);
40+
void js_setglobal(js_State *J, const char *name);
41+
42+
void js_getownproperty(js_State *J, int idx, const char *name);
43+
void js_getproperty(js_State *J, int idx, const char *name);
44+
void js_setproperty(js_State *J, int idx, const char *name);
45+
void js_cfgproperty(js_State *J, int idx, const char *name, int atts);
46+
void js_delproperty(js_State *J, int idx, const char *name);
47+
int js_nextproperty(js_State *J, int idx);
48+
49+
void js_pushglobal(js_State *J);
50+
void js_pushundefined(js_State *J);
51+
void js_pushnull(js_State *J);
52+
void js_pushboolean(js_State *J, int v);
53+
void js_pushnumber(js_State *J, double v);
54+
void js_pushstring(js_State *J, const char *v);
55+
void js_pushliteral(js_State *J, const char *v);
56+
57+
void js_newobject(js_State *J);
58+
void js_newarray(js_State *J);
59+
void js_newcfunction(js_State *J, js_CFunction fun, int length);
60+
61+
int js_isundefined(js_State *J, int idx);
62+
int js_isnull(js_State *J, int idx);
63+
int js_isboolean(js_State *J, int idx);
64+
int js_isnumber(js_State *J, int idx);
65+
int js_isstring(js_State *J, int idx);
66+
int js_isprimitive(js_State *J, int idx);
67+
int js_isobject(js_State *J, int idx);
68+
int js_iscallable(js_State *J, int idx);
69+
70+
int js_toboolean(js_State *J, int idx);
71+
double js_tonumber(js_State *J, int idx);
72+
const char *js_tostring(js_State *J, int idx);
73+
74+
double js_tointeger(js_State *J, int idx);
75+
int js_toint32(js_State *J, int idx);
76+
unsigned int js_touint32(js_State *J, int idx);
77+
short js_toint16(js_State *J, int idx);
78+
unsigned short js_touint16(js_State *J, int idx);
79+
80+
int js_gettop(js_State *J);
81+
void js_settop(js_State *J, int idx);
82+
void js_pop(js_State *J, int n);
83+
void js_copy(js_State *J, int idx);
84+
void js_remove(js_State *J, int idx);
85+
void js_insert(js_State *J, int idx);
86+
void js_replace(js_State* J, int idx);
87+
88+
void js_concat(js_State *J);
89+
int js_compare(js_State *J);
90+
int js_equal(js_State *J);
91+
int js_strictequal(js_State *J);
8592

8693
#endif

jsbarray.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
#include "js.h"
1+
#include "jsi.h"
22
#include "jsobject.h"
3-
#include "jsrun.h"
4-
#include "jsstate.h"
3+
#include "jsbuiltin.h"
54

65
static int jsB_Array(js_State *J, int n) { return 0; }
76
static int jsB_new_Array(js_State *J, int n) { return 0; }

jsbboolean.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
#include "js.h"
1+
#include "jsi.h"
22
#include "jsobject.h"
3-
#include "jsrun.h"
4-
#include "jsstate.h"
3+
#include "jsbuiltin.h"
54

65
static int jsB_new_Boolean(js_State *J, int n)
76
{

jsberror.c

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
#include "js.h"
1+
#include "jsi.h"
22
#include "jsobject.h"
3-
#include "jsrun.h"
4-
#include "jsstate.h"
3+
#include "jsbuiltin.h"
54

65
static int Ep_toString(js_State *J, int n)
76
{
87
js_getproperty(J, 0, "name");
98
js_pushliteral(J, ": ");
10-
jsR_concat(J);
9+
js_concat(J);
1110
js_getproperty(J, 0, "message");
12-
jsR_concat(J);
11+
js_concat(J);
1312
return 1;
1413
}
1514

jsbfunction.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
#include "js.h"
1+
#include "jsi.h"
22
#include "jscompile.h"
33
#include "jsobject.h"
4-
#include "jsrun.h"
5-
#include "jsstate.h"
4+
#include "jsbuiltin.h"
65

76
static int jsB_new_Function(js_State *J, int n) { return 0; }
87
static int jsB_Function(js_State *J, int n) { return 0; }

jsbmath.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
#include "js.h"
1+
#include "jsi.h"
22
#include "jsobject.h"
3-
#include "jsrun.h"
4-
#include "jsstate.h"
3+
#include "jsbuiltin.h"
54

65
static int Math_abs(js_State *J, int nargs) {
76
return js_pushnumber(J, abs(js_tonumber(J, 1))), 1;

jsbnumber.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
#include "js.h"
1+
#include "jsi.h"
22
#include "jsobject.h"
3-
#include "jsrun.h"
4-
#include "jsstate.h"
3+
#include "jsbuiltin.h"
54

65
static int jsB_new_Number(js_State *J, int n)
76
{

jsbobject.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
#include "js.h"
1+
#include "jsi.h"
22
#include "jsobject.h"
3-
#include "jsrun.h"
4-
#include "jsstate.h"
3+
#include "jsbuiltin.h"
54

65
static int jsB_new_Object(js_State *J, int n)
76
{

jsbstring.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
#include "js.h"
1+
#include "jsi.h"
22
#include "jsobject.h"
3-
#include "jsrun.h"
4-
#include "jsstate.h"
3+
#include "jsbuiltin.h"
54
#include "jsutf.h"
65

76
static int jsB_new_String(js_State *J, int n)

jsbuiltin.c

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
#include "js.h"
1+
#include "jsi.h"
22
#include "jsobject.h"
3-
#include "jsrun.h"
4-
#include "jsstate.h"
3+
#include "jsbuiltin.h"
54

65
static int jsB_print(js_State *J, int argc)
76
{
@@ -26,7 +25,7 @@ static int jsB_eval(js_State *J, int argc)
2625
{
2726
if (!js_isstring(J, -1))
2827
return 1;
29-
jsR_loadscript(J, "(eval)", js_tostring(J, -1));
28+
js_loadstring(J, "(eval)", js_tostring(J, -1));
3029
js_copy(J, 0);
3130
js_call(J, 0);
3231
return 1;

jsbuiltin.h

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef js_builtin_h
2+
#define js_builtin_h
3+
4+
void jsB_init(js_State *J);
5+
void jsB_initobject(js_State *J);
6+
void jsB_initarray(js_State *J);
7+
void jsB_initfunction(js_State *J);
8+
void jsB_initboolean(js_State *J);
9+
void jsB_initnumber(js_State *J);
10+
void jsB_initstring(js_State *J);
11+
void jsB_initerror(js_State *J);
12+
void jsB_initmath(js_State *J);
13+
14+
void jsB_propf(js_State *J, const char *name, js_CFunction cfun, int n);
15+
void jsB_propn(js_State *J, const char *name, double number);
16+
void jsB_props(js_State *J, const char *name, const char *string);
17+
18+
#endif

jscompile.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
#include "js.h"
1+
#include "jsi.h"
22
#include "jsparse.h"
33
#include "jscompile.h"
4-
#include "jsstate.h"
54

65
#define cexp js_cexp /* collision with math.h */
76

jsconf.h

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef js_conf_h
2+
#define js_conf_h
3+
4+
#define JS_STACKSIZE 256 /* value stack size */
5+
#define JS_MINSTACK 20 /* at least this much available when entering a function */
6+
#define JS_TRYLIMIT 64 /* exception stack size */
7+
#define JS_GCLIMIT 10000 /* run gc cycle every N allocations */
8+
9+
/* noreturn is a GCC extension */
10+
#ifdef __GNUC__
11+
#define JS_NORETURN __attribute__((noreturn))
12+
#else
13+
#ifdef _MSC_VER
14+
#define JS_NORETURN __declspec(noreturn)
15+
#else
16+
#define JS_NORETURN
17+
#endif
18+
#endif
19+
20+
/* GCC can do type checking of printf strings */
21+
#ifdef __printflike
22+
#define JS_PRINTFLIKE __printflike
23+
#else
24+
#if __GNUC__ > 2 || __GNUC__ == 2 && __GNUC_MINOR__ >= 7
25+
#define JS_PRINTFLIKE(fmtarg, firstvararg) \
26+
__attribute__((__format__ (__printf__, fmtarg, firstvararg)))
27+
#else
28+
#define JS_PRINTFLIKE(fmtarg, firstvararg)
29+
#endif
30+
#endif
31+
32+
#endif

jsdump.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include "js.h"
1+
#include "jsi.h"
22
#include "jsparse.h"
33
#include "jscompile.h"
44
#include "jsobject.h"

jsgc.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
#include "js.h"
1+
#include "jsi.h"
22
#include "jscompile.h"
33
#include "jsobject.h"
44
#include "jsrun.h"
5-
#include "jsstate.h"
65

76
static void jsG_markobject(js_State *J, int mark, js_Object *obj);
87

@@ -147,7 +146,7 @@ void js_gc(js_State *J, int report)
147146
genv, nenv, gfun, nfun, gobj, nobj);
148147
}
149148

150-
void js_close(js_State *J)
149+
void js_freestate(js_State *J)
151150
{
152151
js_Function *fun, *nextfun;
153152
js_Object *obj, *nextobj;
@@ -160,8 +159,9 @@ void js_close(js_State *J)
160159
for (obj = J->gcobj; obj; obj = nextobj)
161160
nextobj = obj->gcnext, jsG_freeobject(J, obj);
162161

163-
js_freestrings(J);
162+
jsS_freestrings(J);
164163

165-
free(J->buf.text);
164+
free(J->lexbuf.text);
165+
free(J->stack);
166166
free(J);
167167
}

0 commit comments

Comments
 (0)