shithub: libmujs

Download patch

ref: a3975e7b0a4a2b694ee635cb9d27b413234c846a
parent: 955fd32be17bf539f4878eddb73827126148583c
author: henesy <henesy.dev@gmail.com>
date: Tue Sep 3 18:55:16 EDT 2024

on jsdtoa ;; jsdate incomplete

--- a/jsarray.c
+++ b/jsarray.c
@@ -61,8 +61,8 @@
 
 static void Ap_join(js_State *J)
 {
-	char * volatile out = NULL;
-	const char * volatile r = NULL;
+	char * volatile out = nil;
+	const char * volatile r = nil;
 	const char *sep;
 	int seplen;
 	int k, n, len, rlen;
@@ -274,7 +274,7 @@
 
 static void Ap_sort(js_State *J)
 {
-	struct sortslot * volatile array = NULL;
+	struct sortslot * volatile array = nil;
 	int i, n, len;
 
 	len = js_getlength(J, 0);
--- a/jsbuiltin.c
+++ b/jsbuiltin.c
@@ -98,7 +98,7 @@
 {
 	/* NOTE: volatile to silence GCC warning about longjmp clobbering a variable */
 	const char * volatile str = str_;
-	js_Buffer *sb = NULL;
+	js_Buffer *sb = nil;
 
 	static const char *HEX = "0123456789ABCDEF";
 
@@ -128,7 +128,7 @@
 {
 	/* NOTE: volatile to silence GCC warning about longjmp clobbering a variable */
 	const char * volatile str = str_;
-	js_Buffer *sb = NULL;
+	js_Buffer *sb = nil;
 	int a, b;
 
 	if (js_try(J)) {
@@ -193,7 +193,7 @@
 void jsB_init(js_State *J)
 {
 	/* Create the prototype objects here, before the constructors */
-	J->Object_prototype = jsV_newobject(J, JS_COBJECT, NULL);
+	J->Object_prototype = jsV_newobject(J, JS_COBJECT, nil);
 	J->Array_prototype = jsV_newobject(J, JS_CARRAY, J->Object_prototype);
 	J->Function_prototype = jsV_newobject(J, JS_CCFUNCTION, J->Object_prototype);
 	J->Boolean_prototype = jsV_newobject(J, JS_CBOOLEAN, J->Object_prototype);
@@ -202,7 +202,7 @@
 	J->Date_prototype = jsV_newobject(J, JS_CDATE, J->Object_prototype);
 
 	J->RegExp_prototype = jsV_newobject(J, JS_CREGEXP, J->Object_prototype);
-	J->RegExp_prototype->u.r.prog = js_regcompx(J->alloc, J->actx, "(?:)", 0, NULL);
+	J->RegExp_prototype->u.r.prog = js_regcompx(J->alloc, J->actx, "(?:)", 0, nil);
 	J->RegExp_prototype->u.r.source = js_strdup(J, "(?:)");
 
 	/* All the native error types */
--- a/jscompile.c
+++ b/jscompile.c
@@ -18,10 +18,10 @@
 	char msgbuf[256];
 
 	va_start(ap, fmt);
-	vsnprintf(msgbuf, 256, fmt, ap);
+	vsnprint(msgbuf, 256, fmt, ap);
 	va_end(ap);
 
-	snprintf(buf, 256, "%s:%d: ", J->filename, node->line);
+	snprint(buf, 256, "%s:%d: ", J->filename, node->line);
 	strcat(buf, msgbuf);
 
 	js_newsyntaxerror(J, buf);
@@ -347,12 +347,12 @@
 			emit(J, F, OP_INITPROP);
 			break;
 		case EXP_PROP_GET:
-			emitfunction(J, F, newfun(J, prop->line, NULL, NULL, kv->c, 0, F->strict, 1));
+			emitfunction(J, F, newfun(J, prop->line, nil, nil, kv->c, 0, F->strict, 1));
 			emitline(J, F, kv);
 			emit(J, F, OP_INITGETTER);
 			break;
 		case EXP_PROP_SET:
-			emitfunction(J, F, newfun(J, prop->line, NULL, kv->b, kv->c, 0, F->strict, 1));
+			emitfunction(J, F, newfun(J, prop->line, nil, kv->b, kv->c, 0, F->strict, 1));
 			emitline(J, F, kv);
 			emit(J, F, OP_INITSETTER);
 			break;
@@ -804,7 +804,7 @@
 		js_free(J, jump);
 		jump = next;
 	}
-	stm->jumps = NULL;
+	stm->jumps = nil;
 }
 
 static int isloop(enum js_AstType T)
@@ -843,7 +843,7 @@
 		}
 		node = node->parent;
 	}
-	return NULL;
+	return nil;
 }
 
 static js_Ast *continuetarget(JF, js_Ast *node, const char *label)
@@ -859,7 +859,7 @@
 		}
 		node = node->parent;
 	}
-	return NULL;
+	return nil;
 }
 
 static js_Ast *returntarget(JF, js_Ast *node)
@@ -869,7 +869,7 @@
 			return node;
 		node = node->parent;
 	}
-	return NULL;
+	return nil;
 }
 
 /* Emit code to rebalance stack and scopes during an abrupt exit */
@@ -1012,7 +1012,7 @@
 
 static void cswitch(JF, js_Ast *ref, js_Ast *head)
 {
-	js_Ast *node, *clause, *def = NULL;
+	js_Ast *node, *clause, *def = nil;
 	int end;
 
 	cexp(J, F, ref);
@@ -1216,7 +1216,7 @@
 			if (!target)
 				jsC_error(J, stm, "break label '%s' not found", stm->a->string);
 		} else {
-			target = breaktarget(J, F, stm->parent, NULL);
+			target = breaktarget(J, F, stm->parent, nil);
 			if (!target)
 				jsC_error(J, stm, "unlabelled break must be inside loop or switch");
 		}
@@ -1232,7 +1232,7 @@
 			if (!target)
 				jsC_error(J, stm, "continue label '%s' not found", stm->a->string);
 		} else {
-			target = continuetarget(J, F, stm->parent, NULL);
+			target = continuetarget(J, F, stm->parent, nil);
 			if (!target)
 				jsC_error(J, stm, "continue must be inside loop");
 		}
@@ -1424,5 +1424,5 @@
 
 js_Function *jsC_compilescript(js_State *J, js_Ast *prog, int default_strict)
 {
-	return newfun(J, prog ? prog->line : 0, NULL, NULL, prog, 1, default_strict, 0);
+	return newfun(J, prog ? prog->line : 0, nil, nil, prog, 1, default_strict, 0);
 }
--- a/jsdate.c
+++ b/jsdate.c
@@ -1,36 +1,24 @@
 #include "jsi.h"
 
-#include <time.h>
+//#include <time.h>
 
-#if defined(__unix__) || defined(__APPLE__)
-#include <sys/time.h>
-#elif defined(_WIN32)
-#include <sys/timeb.h>
-#endif
 
 #define js_optnumber(J,I,V) (js_isdefined(J,I) ? js_tonumber(J,I) : V)
 
 static double Now(void)
 {
-#if defined(__unix__) || defined(__APPLE__)
-	struct timeval tv;
-	gettimeofday(&tv, NULL);
-	return floor(tv.tv_sec * 1000.0 + tv.tv_usec / 1000.0);
-#elif defined(_WIN32)
-	struct _timeb tv;
-	_ftime(&tv);
-	return tv.time * 1000.0 + tv.millitm;
-#else
-	return time(NULL) * 1000.0;
-#endif
+	// * 1000.0;
+	return time(0);
 }
 
+
+/*
 static double LocalTZA(void)
 {
 	static int once = 1;
 	static double tza = 0;
 	if (once) {
-		time_t now = time(NULL);
+		time_t now = time(0);
 		time_t utc = mktime(gmtime(&now));
 		time_t loc = mktime(localtime(&now));
 		tza = (loc - utc) * 1000;
@@ -41,10 +29,10 @@
 
 static double DaylightSavingTA(double t)
 {
-	return 0; /* TODO */
+	return 0; // TODO 
 }
 
-/* Helpers from the ECMA 262 specification */
+// Helpers from the ECMA 262 specification 
 
 #define HoursPerDay		24.0
 #define MinutesPerDay		(HoursPerDay * MinutesPerHour)
@@ -195,10 +183,8 @@
 
 static double MakeDay(double y, double m, double date)
 {
-	/*
-	 * The following array contains the day of year for the first day of
-	 * each month, where index 0 is January, and day 0 is January 1.
-	 */
+	// The following array contains the day of year for the first day of each month, where index 0 is January, and day 0 is January 1.
+	 
 	static const double firstDayOfMonth[2][12] = {
 		{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334},
 		{0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}
@@ -253,8 +239,8 @@
 	int tza = 0;
 	double t;
 
-	/* Parse ISO 8601 formatted date and time: */
-	/* YYYY("-"MM("-"DD)?)?("T"HH":"mm(":"ss("."sss)?)?("Z"|[+-]HH(":"mm)?)?)? */
+	// Parse ISO 8601 formatted date and time: 
+	// YYYY("-"MM("-"DD)?)?("T"HH":"mm(":"ss("."sss)?)?("Z"|[+-]HH(":"mm)?)?)? 
 
 	if (!toint(&s, 4, &y)) return NAN;
 	if (*s == '-') {
@@ -309,12 +295,12 @@
 	if (ms < 0 || ms > 999) return NAN;
 	if (H == 24 && (M != 0 || S != 0 || ms != 0)) return NAN;
 
-	/* TODO: DaylightSavingTA on local times */
+	// TODO: DaylightSavingTA on local times 
 	t = MakeDate(MakeDay(y, m-1, d), MakeTime(H, M, S, ms));
 	return t - tza;
 }
 
-/* date formatting */
+// date formatting
 
 static char *fmtdate(char *buf, double t)
 {
@@ -357,7 +343,7 @@
 	return buf;
 }
 
-/* Date functions */
+// Date functions
 
 static double js_todate(js_State *J, int idx)
 {
@@ -845,17 +831,19 @@
 		jsB_propf(J, "Date.prototype.setFullYear", Dp_setFullYear, 3);
 		jsB_propf(J, "Date.prototype.setUTCFullYear", Dp_setUTCFullYear, 3);
 
-		/* ES5 */
+		// ES5
 		jsB_propf(J, "Date.prototype.toISOString", Dp_toISOString, 0);
 		jsB_propf(J, "Date.prototype.toJSON", Dp_toJSON, 1);
 	}
-	js_newcconstructor(J, jsB_Date, jsB_new_Date, "Date", 0); /* 1 */
+	js_newcconstructor(J, jsB_Date, jsB_new_Date, "Date", 0); // 1 
 	{
 		jsB_propf(J, "Date.parse", D_parse, 1);
 		jsB_propf(J, "Date.UTC", D_UTC, 7);
 
-		/* ES5 */
+		// ES5 
 		jsB_propf(J, "Date.now", D_now, 0);
 	}
 	js_defglobal(J, "Date", JS_DONTENUM);
 }
+
+*/
\ No newline at end of file
--- a/jsdtoa.c
+++ b/jsdtoa.c
@@ -2,15 +2,8 @@
 
 #include "jsi.h"
 
-#if defined(_MSC_VER) && (_MSC_VER < 1700) /* VS2012 has stdint.h */
-typedef unsigned int uint32_t;
-typedef unsigned __int64 uint64_t;
-#else
-#include <stdint.h>
-#endif
-
-#include <errno.h>
-#include <assert.h>
+//#include <errno.h>
+//#include <assert.h>
 
 #ifndef TRUE
 #define TRUE 1
--- a/jsi.h
+++ b/jsi.h
@@ -3,16 +3,6 @@
 
 #include "mujs.h"
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <stddef.h>
-#include <stdarg.h>
-#include <string.h>
-#include <setjmp.h>
-#include <math.h>
-#include <float.h>
-#include <limits.h>
-
 /* NOTE: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103052 */
 #ifdef __GNUC__
 #if (__GNUC__ >= 6)
@@ -27,38 +17,10 @@
 #pragma warning(disable:4267) /* implicit conversion of int to smaller int */
 #pragma warning(disable:4090) /* broken const warnings */
 #define inline __inline
-#if _MSC_VER < 1900 /* MSVC 2015 */
-#define snprintf jsW_snprintf
-#define vsnprintf jsW_vsnprintf
-static int jsW_vsnprintf(char *str, size_t size, const char *fmt, va_list ap)
-{
-	int n;
-	n = _vsnprintf(str, size, fmt, ap);
-	str[size-1] = 0;
-	return n;
-}
-static int jsW_snprintf(char *str, size_t size, const char *fmt, ...)
-{
-	int n;
-	va_list ap;
-	va_start(ap, fmt);
-	n = jsW_vsnprintf(str, size, fmt, ap);
-	va_end(ap);
-	return n;
-}
 #endif
-#if _MSC_VER <= 1700 /* <= MSVC 2012 */
-#define isnan(x) _isnan(x)
-#define isinf(x) (!_finite(x))
-#define isfinite(x) _finite(x)
-static __inline int signbit(double x) { __int64 i; memcpy(&i, &x, 8); return i>>63; }
-#define INFINITY (DBL_MAX+DBL_MAX)
-#define NAN (INFINITY-INFINITY)
-#endif
-#endif
 
 #define soffsetof(x,y) ((int)offsetof(x,y))
-#define nelem(a) (int)(sizeof (a) / sizeof (a)[0])
+//#define nelem(a) (int)(sizeof (a) / sizeof (a)[0])
 
 void *js_malloc(js_State *J, int size);
 void *js_realloc(js_State *J, void *ptr, int size);
--- a/mujs.h
+++ b/mujs.h
@@ -1,8 +1,56 @@
 #ifndef mujs_h
 #define mujs_h
 
-#include <setjmp.h> /* required for setjmp in fz_try macro */
 
+#include <u.h>
+#include <libc.h>
+
+#define CHAR_BIT	8
+#define MB_LEN_MAX	4
+
+#define UCHAR_MAX	0xff
+#define USHRT_MAX	0xffff
+#define UINT_MAX	0xffffffffU
+#define ULONG_MAX	0xffffffffUL
+#define ULLONG_MAX	0xffffffffffffffffULL
+
+#define CHAR_MAX	SCHAR_MAX
+#define SCHAR_MAX	0x7f
+#define SHRT_MAX	0x7fff
+#define INT_MAX		0x7fffffff
+#define LONG_MAX	0x7fffffffL
+#define LLONG_MAX	0x7fffffffffffffffLL
+
+#define CHAR_MIN	SCHAR_MIN
+#define SCHAR_MIN	(-SCHAR_MAX-1)
+#define SHRT_MIN	(-SHRT_MAX-1)
+#define INT_MIN		(-INT_MAX-1)
+#define LONG_MIN	(-LONG_MAX-1)
+#define LLONG_MIN	(-LLONG_MAX-1)
+
+// https://learn.microsoft.com/en-us/cpp/c-runtime-library/data-type-constants?view=msvc-170
+#define DBL_MAX 1.79769313486231570815e+308L
+
+#define isnan(x) isNaN(x)
+#define INFINITY (DBL_MAX+DBL_MAX)
+#define NAN (INFINITY-INFINITY)
+
+#define size_t		usize
+
+#define signbit(x) (x < 0.0)
+
+#define int8_t		s8int
+#define int16_t		s16int
+#define int32_t		s32int
+#define int64_t		s64int
+
+#define uint8_t		u8int
+#define uint16_t	u16int
+#define uint32_t	u32int
+#define uint64_t	u64int
+
+//#include <setjmp.h> /* required for setjmp in fz_try macro */
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -14,28 +62,9 @@
 #define JS_VERSION (JS_VERSION_MAJOR * 10000 + JS_VERSION_MINOR * 100 + JS_VERSION_PATCH)
 #define JS_CHECKVERSION(x,y,z) (JS_VERSION >= ((x) * 10000 + (y) * 100 + (z)))
 
-/* noreturn is a GCC extension */
-#ifdef __GNUC__
-#define JS_NORETURN __attribute__((noreturn))
-#else
-#ifdef _MSC_VER
-#define JS_NORETURN __declspec(noreturn)
-#else
 #define JS_NORETURN
-#endif
-#endif
 
-/* GCC can do type checking of printf strings */
-#ifdef __printflike
-#define JS_PRINTFLIKE __printflike
-#else
-#if __GNUC__ > 2 || __GNUC__ == 2 && __GNUC_MINOR__ >= 7
-#define JS_PRINTFLIKE(fmtarg, firstvararg) \
-	__attribute__((__format__ (__printf__, fmtarg, firstvararg)))
-#else
 #define JS_PRINTFLIKE(fmtarg, firstvararg)
-#endif
-#endif
 
 typedef struct js_State js_State;