From 7268e45c0fe4c3300247b07c7af3b3dea7a8dfbb Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Sun, 7 Feb 2021 21:51:12 -0800 Subject: [PATCH] Simplify hiwire constants (#1213) Previously they were defined as macros and getters were defined for use in javascript, since javascript cannot access macros. Now both are replaced by global constants. --- src/core/hiwire.c | 37 +++++++++---------------------------- src/core/hiwire.h | 40 ++++------------------------------------ src/core/jsproxy.c | 2 +- src/core/pyproxy.c | 4 ++-- src/core/python2js.c | 6 +++--- src/core/runpython.c | 4 ++-- 6 files changed, 21 insertions(+), 72 deletions(-) diff --git a/src/core/hiwire.c b/src/core/hiwire.c index 933cf6250..5f5302ff1 100644 --- a/src/core/hiwire.c +++ b/src/core/hiwire.c @@ -6,34 +6,15 @@ #include "hiwire.h" -JsRef -hiwire_undefined() -{ - return Js_UNDEFINED; -} - -JsRef -hiwire_null() -{ - return Js_NULL; -} - -JsRef -hiwire_true() -{ - return Js_TRUE; -} - -JsRef -hiwire_false() -{ - return Js_FALSE; -} +const JsRef Js_undefined = ((JsRef)(2)); +const JsRef Js_true = ((JsRef)(4)); +const JsRef Js_false = ((JsRef)(6)); +const JsRef Js_null = ((JsRef)(8)); JsRef hiwire_bool(bool boolean) { - return boolean ? hiwire_true() : hiwire_false(); + return boolean ? Js_true : Js_false; } EM_JS(int, hiwire_init, (), { @@ -52,10 +33,10 @@ EM_JS(int, hiwire_init, (), { counter : new Uint32Array([1]) }; Module.hiwire = {}; - Module.hiwire.UNDEFINED = _hiwire_undefined(); - Module.hiwire.JSNULL = _hiwire_null(); - Module.hiwire.TRUE = _hiwire_true(); - Module.hiwire.FALSE = _hiwire_false(); + Module.hiwire.UNDEFINED = HEAP8[_Js_undefined]; + Module.hiwire.JSNULL = HEAP8[_Js_null]; + Module.hiwire.TRUE = HEAP8[_Js_true]; + Module.hiwire.FALSE = HEAP8[_Js_false]; _hiwire.objects.set(Module.hiwire.UNDEFINED, undefined); _hiwire.objects.set(Module.hiwire.JSNULL, null); diff --git a/src/core/hiwire.h b/src/core/hiwire.h index 3a3f7537e..2941329a9 100644 --- a/src/core/hiwire.h +++ b/src/core/hiwire.h @@ -39,10 +39,10 @@ typedef struct _JsRefStruct* JsRef; // Special JsRefs for singleton constants. // (These must be even because the least significance bit is set to 0 for // singleton constants.) -#define Js_UNDEFINED ((JsRef)(2)) -#define Js_TRUE ((JsRef)(4)) -#define Js_FALSE ((JsRef)(6)) -#define Js_NULL ((JsRef)(8)) +extern const JsRef Js_undefined; +extern const JsRef Js_true; +extern const JsRef Js_false; +extern const JsRef Js_null; #define hiwire_CLEAR(x) \ do { \ @@ -231,38 +231,6 @@ hiwire_float32array(f32* ptr, int len); JsRef hiwire_float64array(f64* ptr, int len); -/** - * Create a new Javascript undefined value. - * - * Returns: "New" reference - */ -JsRef -hiwire_undefined(); - -/** - * Create a new Javascript null value. - * - * Returns: "New" reference - */ -JsRef -hiwire_null(); - -/** - * Create a new Javascript true value. - * - * Returns: "New" reference - */ -JsRef -hiwire_true(); - -/** - * Create a new Javascript false value. - * - * Returns: "New" reference - */ -JsRef -hiwire_false(); - /** * Create a new Javascript boolean value. * Return value is true if boolean != 0, false if boolean == 0. diff --git a/src/core/jsproxy.c b/src/core/jsproxy.c index 46f0fdb8a..6ab0eb6e6 100644 --- a/src/core/jsproxy.c +++ b/src/core/jsproxy.c @@ -825,7 +825,7 @@ JsProxy_create(JsRef object) if (hiwire_is_error(object)) { return JsProxy_new_error(object); } else if (hiwire_is_function(object)) { - return JsMethod_cnew(object, hiwire_null()); + return JsMethod_cnew(object, Js_null); } else if (hiwire_is_typedarray(object)) { return JsBuffer_cnew(object); } else { diff --git a/src/core/pyproxy.c b/src/core/pyproxy.c index 91c816627..11a746c21 100644 --- a/src/core/pyproxy.c +++ b/src/core/pyproxy.c @@ -45,7 +45,7 @@ _pyproxy_get(PyObject* pyobj, JsRef idkey) Py_DECREF(pykey); if (pyattr == NULL) { PyErr_Clear(); - return hiwire_undefined(); + return Js_undefined; } JsRef idattr = python2js(pyattr); @@ -91,7 +91,7 @@ _pyproxy_deleteProperty(PyObject* pyobj, JsRef idkey) return NULL; } - return hiwire_undefined(); + return Js_undefined; } JsRef diff --git a/src/core/python2js.c b/src/core/python2js.c index f557f42a6..77ebc5491 100644 --- a/src/core/python2js.c +++ b/src/core/python2js.c @@ -233,11 +233,11 @@ static JsRef _python2js_immutable(PyObject* x, PyObject* map, int depth) { if (x == Py_None) { - return hiwire_undefined(); + return Js_undefined; } else if (x == Py_True) { - return hiwire_true(); + return Js_true; } else if (x == Py_False) { - return hiwire_false(); + return Js_false; } else if (PyLong_Check(x)) { return _python2js_long(x); } else if (PyFloat_Check(x)) { diff --git a/src/core/runpython.c b/src/core/runpython.c index 40d37b57e..5d0f8bab6 100644 --- a/src/core/runpython.c +++ b/src/core/runpython.c @@ -21,7 +21,7 @@ _runPythonDebug(char* code) if (py_code == NULL) { fprintf(stderr, "runPythonDebug -- error occurred converting argument:\n"); PyErr_Print(); - return Js_UNDEFINED; + return Js_undefined; } PyObject* result = _PyObject_CallMethodIdObjArgs( @@ -30,7 +30,7 @@ _runPythonDebug(char* code) if (result == NULL) { fprintf(stderr, "runPythonDebug -- error occurred\n"); PyErr_Print(); - return Js_UNDEFINED; + return Js_undefined; } printf("runPythonDebug -- eval_code succeeded, it returned:\n");