shithub: libmujs

ref: 060dbbb03ca18062b4d6c3b0666b5924c592350b
dir: /jsbuiltin.c/

View raw version
#include "js.h"
#include "jsobject.h"
#include "jsrun.h"
#include "jsstate.h"

static int jsB_print(js_State *J, int argc)
{
	int i;
	for (i = 1; i < argc; ++i) {
		const char *s = js_tostring(J, i);
		if (i > 1) putchar(' ');
		fputs(s, stdout);
	}
	putchar('\n');
	return 0;
}

static int jsB_eval(js_State *J, int argc)
{
	const char *s;

	if (!js_isstring(J, -1))
		return 1;

	// FIXME: return value if eval string is an expression

	s = js_tostring(J, -1);
	if (jsR_loadscript(J, "(eval)", s))
		jsR_error(J, "SyntaxError (eval)");

	js_dup(J, 0); /* copy this */
	js_call(J, 0);
	return 1;
}

static int jsB_Object(js_State *J, int n) { return 0; }
static int jsB_Array(js_State *J, int n) { return 0; }
static int jsB_Function(js_State *J, int n) { return 0; }
static int jsB_Boolean(js_State *J, int n) { return 0; }
static int jsB_Number(js_State *J, int n) { return 0; }
static int jsB_String(js_State *J, int n) { return 0; }

static void jsB_initobject(js_State *J)
{
	J->Object_prototype = jsR_newobject(J, JS_COBJECT, NULL);
	js_pushcfunction(J, jsB_Object);
	js_pushobject(J, J->Object_prototype);
	js_setproperty(J, -2, "prototype");
	js_setglobal(J, "Object");
}

static void jsB_initarray(js_State *J)
{
	J->Array_prototype = jsR_newobject(J, JS_COBJECT, NULL);
	js_pushcfunction(J, jsB_Array);
	js_pushobject(J, J->Array_prototype);
	js_setproperty(J, -2, "prototype");
	js_setglobal(J, "Array");
}

static void jsB_initfunction(js_State *J)
{
	J->Function_prototype = jsR_newobject(J, JS_COBJECT, NULL);
	js_pushcfunction(J, jsB_Function);
	js_pushobject(J, J->Function_prototype);
	js_setproperty(J, -2, "prototype");
	js_setglobal(J, "Function");
}

static void jsB_initboolean(js_State *J)
{
	J->Boolean_prototype = jsR_newobject(J, JS_COBJECT, NULL);
	js_pushcfunction(J, jsB_Boolean);
	js_pushobject(J, J->Boolean_prototype);
	js_setproperty(J, -2, "prototype");
	js_setglobal(J, "Boolean");
}

static void jsB_initnumber(js_State *J)
{
	J->Number_prototype = jsR_newobject(J, JS_COBJECT, NULL);
	js_pushcfunction(J, jsB_Number);
	js_pushobject(J, J->Number_prototype);
	js_setproperty(J, -2, "prototype");
	js_setglobal(J, "Number");
}

static void jsB_initstring(js_State *J)
{
	J->String_prototype = jsR_newobject(J, JS_COBJECT, NULL);
	js_pushcfunction(J, jsB_String);
	js_pushobject(J, J->String_prototype);
	js_setproperty(J, -2, "prototype");
	js_setglobal(J, "String");
}

static void jsB_register(js_State *J, const char *name, js_CFunction cfun)
{
	js_pushcfunction(J, cfun);
	js_setglobal(J, name);
}

void jsB_init(js_State *J)
{
	jsB_initobject(J);
	jsB_initarray(J);
	jsB_initfunction(J);
	jsB_initboolean(J);
	jsB_initnumber(J);
	jsB_initstring(J);

	jsB_register(J, "eval", jsB_eval);
	jsB_register(J, "print", jsB_print);
}