~ft/mujs

f9452532fdff396afe7916512de8e526025a3558 — Sigrid Solveig Haflínudóttir 2 months ago c3715ce master
Plan 9 port
10 files changed, 89 insertions(+), 20 deletions(-)

M jserror.c
M jsi.h
M jsparse.c
M jsrun.c
M jsvalue.h
A mkfile
M mujs.h
M regexp.c
M regexp.h
M utf.h
M jserror.c => jserror.c +2 -1
@@ 83,7 83,7 @@ static void js_newerrorx(js_State *J, const char *message, js_Object *prototype)
	void js_new##name(js_State *J, const char *s) { \
		js_newerrorx(J, s, J->Name##_prototype); \
	} \
	void js_##name(js_State *J, const char *fmt, ...) { \
	void *js_##name(js_State *J, const char *fmt, ...) { \
		va_list ap; \
		char buf[256]; \
		va_start(ap, fmt); \


@@ 91,6 91,7 @@ static void js_newerrorx(js_State *J, const char *message, js_Object *prototype)
		va_end(ap); \
		js_newerrorx(J, buf, J->Name##_prototype); \
		js_throw(J); \
		return NULL; \
	}

DERROR(error, Error)

M jsi.h => jsi.h +4 -0
@@ 51,7 51,9 @@ static __inline int signbit(double x) { __int64 i; memcpy(&i, &x, 8); return i>>
#endif

#define soffsetof(x,y) ((int)offsetof(x,y))
#ifndef nelem
#define nelem(a) (int)(sizeof (a) / sizeof (a)[0])
#endif

void *js_malloc(js_State *J, int size);
void *js_realloc(js_State *J, void *ptr, int size);


@@ 63,7 65,9 @@ typedef struct js_Object js_Object;
typedef struct js_String js_String;
typedef struct js_Ast js_Ast;
typedef struct js_Function js_Function;
#pragma incomplete js_Function
typedef struct js_Environment js_Environment;
#pragma incomplete js_Environment
typedef struct js_StringNode js_StringNode;
typedef struct js_Jumpbuf js_Jumpbuf;
typedef struct js_StackTrace js_StackTrace;

M jsparse.c => jsparse.c +11 -9
@@ 21,14 21,14 @@ static js_Ast *memberexp(js_State *J);
static js_Ast *statement(js_State *J);
static js_Ast *funbody(js_State *J);

JS_NORETURN static void jsP_error(js_State *J, const char *fmt, ...) JS_PRINTFLIKE(2,3);
JS_NORETURN static void *jsP_error(js_State *J, const char *fmt, ...) JS_PRINTFLIKE(2,3);

#define INCREC() if (++J->astdepth > JS_ASTLIMIT) jsP_error(J, "too much recursion")
#define DECREC() --J->astdepth
#define SAVEREC() int SAVE=J->astdepth
#define POPREC() J->astdepth=SAVE

static void jsP_error(js_State *J, const char *fmt, ...)
static void *jsP_error(js_State *J, const char *fmt, ...)
{
	va_list ap;
	char buf[512];


@@ 43,6 43,8 @@ static void jsP_error(js_State *J, const char *fmt, ...)

	js_newsyntaxerror(J, buf);
	js_throw(J);

	return NULL;
}

static void jsP_warning(js_State *J, const char *fmt, ...)


@@ 165,7 167,7 @@ static js_Ast *identifier(js_State *J)
		jsP_next(J);
		return a;
	}
	jsP_error(J, "unexpected token: %s (expected identifier)", jsY_tokenstring(J->lookahead));
	return jsP_error(J, "unexpected token: %s (expected identifier)", jsY_tokenstring(J->lookahead));
}

static js_Ast *identifieropt(js_State *J)


@@ 182,7 184,7 @@ static js_Ast *identifiername(js_State *J)
		jsP_next(J);
		return a;
	}
	jsP_error(J, "unexpected token: %s (expected identifier or keyword)", jsY_tokenstring(J->lookahead));
	return jsP_error(J, "unexpected token: %s (expected identifier or keyword)", jsY_tokenstring(J->lookahead));
}

static js_Ast *arrayelement(js_State *J)


@@ 362,7 364,7 @@ static js_Ast *primary(js_State *J)
		return a;
	}

	jsP_error(J, "unexpected token in expression: %s", jsY_tokenstring(J->lookahead));
	return jsP_error(J, "unexpected token in expression: %s", jsY_tokenstring(J->lookahead));
}

static js_Ast *arguments(js_State *J)


@@ 699,7 701,7 @@ static js_Ast *caseclause(js_State *J)
		return STM1(DEFAULT, a);
	}

	jsP_error(J, "unexpected token in switch: %s (expected 'case' or 'default')", jsY_tokenstring(J->lookahead));
	return jsP_error(J, "unexpected token in switch: %s (expected 'case' or 'default')", jsY_tokenstring(J->lookahead));
}

static js_Ast *caselist(js_State *J)


@@ 750,7 752,7 @@ static js_Ast *forstatement(js_State *J, int line)
			c = statement(J);
			return STM3(FOR_IN_VAR, a, b, c);
		}
		jsP_error(J, "unexpected token in for-var-statement: %s", jsY_tokenstring(J->lookahead));
		return jsP_error(J, "unexpected token in for-var-statement: %s", jsY_tokenstring(J->lookahead));
	}

	if (J->lookahead != ';')


@@ 769,7 771,7 @@ static js_Ast *forstatement(js_State *J, int line)
		c = statement(J);
		return STM3(FOR_IN, a, b, c);
	}
	jsP_error(J, "unexpected token in for-statement: %s", jsY_tokenstring(J->lookahead));
	return jsP_error(J, "unexpected token in for-statement: %s", jsY_tokenstring(J->lookahead));
}

static js_Ast *statement(js_State *J)


@@ 887,7 889,7 @@ static js_Ast *statement(js_State *J)
			d = block(J);
		}
		if (!b && !d)
			jsP_error(J, "unexpected token in try: %s (expected 'catch' or 'finally')", jsY_tokenstring(J->lookahead));
			return jsP_error(J, "unexpected token in try: %s (expected 'catch' or 'finally')", jsY_tokenstring(J->lookahead));
		stm = STM4(TRY, a, b, c, d);
	}


M jsrun.c => jsrun.c +3 -3
@@ 323,7 323,7 @@ js_Regexp *js_toregexp(js_State *J, int idx)
	js_Value *v = stackidx(J, idx);
	if (v->type == JS_TOBJECT && v->u.object->type == JS_CREGEXP)
		return &v->u.object->u.r;
	js_typeerror(J, "not a regexp");
	return js_typeerror(J, "not a regexp");
}

void *js_touserdata(js_State *J, int idx, const char *tag)


@@ 332,7 332,7 @@ void *js_touserdata(js_State *J, int idx, const char *tag)
	if (v->type == JS_TOBJECT && v->u.object->type == JS_CUSERDATA)
		if (!strcmp(tag, v->u.object->u.user.tag))
			return v->u.object->u.user.data;
	js_typeerror(J, "not a %s", tag);
	return js_typeerror(J, "not a %s", tag);
}

static js_Object *jsR_tofunction(js_State *J, int idx)


@@ 343,7 343,7 @@ static js_Object *jsR_tofunction(js_State *J, int idx)
	if (v->type == JS_TOBJECT)
		if (v->u.object->type == JS_CFUNCTION || v->u.object->type == JS_CCFUNCTION)
			return v->u.object;
	js_typeerror(J, "not a function");
	return js_typeerror(J, "not a function");
}

/* Stack manipulation */

M jsvalue.h => jsvalue.h +1 -0
@@ 122,6 122,7 @@ struct js_Object
	js_Object *gcroot; /* scan list */
	int gcmark;
};
#pragma incomplete js_Object

struct js_Property
{

A mkfile => mkfile +53 -0
@@ 0,0 1,53 @@
</$objtype/mkfile

BIN=/$objtype/bin
TARG=mujs
CFLAGS=$CFLAGS -p -I/sys/include/npe -D__plan9__

HFILES=\
	astnames.h\
	jsbuiltin.h\
	jscompile.h\
	jsi.h\
	jslex.h\
	jsparse.h\
	jsrun.h\
	jsvalue.h\
	mujs.h\
	opnames.h\
	regexp.h\
	utf.h\

OFILES=\
	jsarray.$O\
	jsboolean.$O\
	jsbuiltin.$O\
	jscompile.$O\
	jsdate.$O\
	jsdtoa.$O\
	jsdump.$O\
	jserror.$O\
	jsfunction.$O\
	jsgc.$O\
	jsintern.$O\
	jslex.$O\
	jsmath.$O\
	jsnumber.$O\
	jsobject.$O\
	json.$O\
	jsparse.$O\
	jsproperty.$O\
	jsregexp.$O\
	jsrepr.$O\
	jsrun.$O\
	jsstate.$O\
	jsstring.$O\
	jsvalue.$O\
	main.$O\
	regexp.$O\

default:V: all

CLEANFILES=$OFILES

</sys/src/cmd/mkone

M mujs.h => mujs.h +8 -7
@@ 31,6 31,7 @@ extern "C" {
#endif

typedef struct js_State js_State;
#pragma incomplete js_State

typedef void *(*js_Alloc)(void *memctx, void *ptr, int size);
typedef void (*js_Panic)(js_State *J);


@@ 106,13 107,13 @@ void js_newsyntaxerror(js_State *J, const char *message);
void js_newtypeerror(js_State *J, const char *message);
void js_newurierror(js_State *J, const char *message);

JS_NORETURN void js_error(js_State *J, const char *fmt, ...) JS_PRINTFLIKE(2,3);
JS_NORETURN void js_evalerror(js_State *J, const char *fmt, ...) JS_PRINTFLIKE(2,3);
JS_NORETURN void js_rangeerror(js_State *J, const char *fmt, ...) JS_PRINTFLIKE(2,3);
JS_NORETURN void js_referenceerror(js_State *J, const char *fmt, ...) JS_PRINTFLIKE(2,3);
JS_NORETURN void js_syntaxerror(js_State *J, const char *fmt, ...) JS_PRINTFLIKE(2,3);
JS_NORETURN void js_typeerror(js_State *J, const char *fmt, ...) JS_PRINTFLIKE(2,3);
JS_NORETURN void js_urierror(js_State *J, const char *fmt, ...) JS_PRINTFLIKE(2,3);
JS_NORETURN void *js_error(js_State *J, const char *fmt, ...) JS_PRINTFLIKE(2,3);
JS_NORETURN void *js_evalerror(js_State *J, const char *fmt, ...) JS_PRINTFLIKE(2,3);
JS_NORETURN void *js_rangeerror(js_State *J, const char *fmt, ...) JS_PRINTFLIKE(2,3);
JS_NORETURN void *js_referenceerror(js_State *J, const char *fmt, ...) JS_PRINTFLIKE(2,3);
JS_NORETURN void *js_syntaxerror(js_State *J, const char *fmt, ...) JS_PRINTFLIKE(2,3);
JS_NORETURN void *js_typeerror(js_State *J, const char *fmt, ...) JS_PRINTFLIKE(2,3);
JS_NORETURN void *js_urierror(js_State *J, const char *fmt, ...) JS_PRINTFLIKE(2,3);
JS_NORETURN void js_throw(js_State *J);

void js_loadstring(js_State *J, const char *filename, const char *source);

M regexp.c => regexp.c +2 -0
@@ 11,7 11,9 @@
#define next regnext
#define accept regaccept

#ifndef nelem
#define nelem(a) (int)(sizeof (a) / sizeof (a)[0])
#endif

#define REPINF 255
#ifndef REG_MAXPROG

M regexp.h => regexp.h +1 -0
@@ 8,6 8,7 @@
#define regfree js_regfree

typedef struct Reprog Reprog;
#pragma incomplete Reprog
typedef struct Resub Resub;

Reprog *regcompx(void *(*alloc)(void *ctx, void *p, int n), void *ctx,

M utf.h => utf.h +4 -0
@@ 1,6 1,8 @@
#ifndef js_utf_h
#define js_utf_h

#ifndef __plan9__

typedef int Rune;	/* 32 bits */

#define chartorune	jsU_chartorune


@@ 41,3 43,5 @@ Rune		totitlerune(Rune c);
Rune		toupperrune(Rune c);

#endif

#endif