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.
This commit is contained in:
Hood Chatham 2021-02-07 21:51:12 -08:00 committed by GitHub
parent 5d50a5f96a
commit 7268e45c0f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 21 additions and 72 deletions

View File

@ -6,34 +6,15 @@
#include "hiwire.h" #include "hiwire.h"
JsRef const JsRef Js_undefined = ((JsRef)(2));
hiwire_undefined() const JsRef Js_true = ((JsRef)(4));
{ const JsRef Js_false = ((JsRef)(6));
return Js_UNDEFINED; const JsRef Js_null = ((JsRef)(8));
}
JsRef
hiwire_null()
{
return Js_NULL;
}
JsRef
hiwire_true()
{
return Js_TRUE;
}
JsRef
hiwire_false()
{
return Js_FALSE;
}
JsRef JsRef
hiwire_bool(bool boolean) hiwire_bool(bool boolean)
{ {
return boolean ? hiwire_true() : hiwire_false(); return boolean ? Js_true : Js_false;
} }
EM_JS(int, hiwire_init, (), { EM_JS(int, hiwire_init, (), {
@ -52,10 +33,10 @@ EM_JS(int, hiwire_init, (), {
counter : new Uint32Array([1]) counter : new Uint32Array([1])
}; };
Module.hiwire = {}; Module.hiwire = {};
Module.hiwire.UNDEFINED = _hiwire_undefined(); Module.hiwire.UNDEFINED = HEAP8[_Js_undefined];
Module.hiwire.JSNULL = _hiwire_null(); Module.hiwire.JSNULL = HEAP8[_Js_null];
Module.hiwire.TRUE = _hiwire_true(); Module.hiwire.TRUE = HEAP8[_Js_true];
Module.hiwire.FALSE = _hiwire_false(); Module.hiwire.FALSE = HEAP8[_Js_false];
_hiwire.objects.set(Module.hiwire.UNDEFINED, undefined); _hiwire.objects.set(Module.hiwire.UNDEFINED, undefined);
_hiwire.objects.set(Module.hiwire.JSNULL, null); _hiwire.objects.set(Module.hiwire.JSNULL, null);

View File

@ -39,10 +39,10 @@ typedef struct _JsRefStruct* JsRef;
// Special JsRefs for singleton constants. // Special JsRefs for singleton constants.
// (These must be even because the least significance bit is set to 0 for // (These must be even because the least significance bit is set to 0 for
// singleton constants.) // singleton constants.)
#define Js_UNDEFINED ((JsRef)(2)) extern const JsRef Js_undefined;
#define Js_TRUE ((JsRef)(4)) extern const JsRef Js_true;
#define Js_FALSE ((JsRef)(6)) extern const JsRef Js_false;
#define Js_NULL ((JsRef)(8)) extern const JsRef Js_null;
#define hiwire_CLEAR(x) \ #define hiwire_CLEAR(x) \
do { \ do { \
@ -231,38 +231,6 @@ hiwire_float32array(f32* ptr, int len);
JsRef JsRef
hiwire_float64array(f64* ptr, int len); 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. * Create a new Javascript boolean value.
* Return value is true if boolean != 0, false if boolean == 0. * Return value is true if boolean != 0, false if boolean == 0.

View File

@ -825,7 +825,7 @@ JsProxy_create(JsRef object)
if (hiwire_is_error(object)) { if (hiwire_is_error(object)) {
return JsProxy_new_error(object); return JsProxy_new_error(object);
} else if (hiwire_is_function(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)) { } else if (hiwire_is_typedarray(object)) {
return JsBuffer_cnew(object); return JsBuffer_cnew(object);
} else { } else {

View File

@ -45,7 +45,7 @@ _pyproxy_get(PyObject* pyobj, JsRef idkey)
Py_DECREF(pykey); Py_DECREF(pykey);
if (pyattr == NULL) { if (pyattr == NULL) {
PyErr_Clear(); PyErr_Clear();
return hiwire_undefined(); return Js_undefined;
} }
JsRef idattr = python2js(pyattr); JsRef idattr = python2js(pyattr);
@ -91,7 +91,7 @@ _pyproxy_deleteProperty(PyObject* pyobj, JsRef idkey)
return NULL; return NULL;
} }
return hiwire_undefined(); return Js_undefined;
} }
JsRef JsRef

View File

@ -233,11 +233,11 @@ static JsRef
_python2js_immutable(PyObject* x, PyObject* map, int depth) _python2js_immutable(PyObject* x, PyObject* map, int depth)
{ {
if (x == Py_None) { if (x == Py_None) {
return hiwire_undefined(); return Js_undefined;
} else if (x == Py_True) { } else if (x == Py_True) {
return hiwire_true(); return Js_true;
} else if (x == Py_False) { } else if (x == Py_False) {
return hiwire_false(); return Js_false;
} else if (PyLong_Check(x)) { } else if (PyLong_Check(x)) {
return _python2js_long(x); return _python2js_long(x);
} else if (PyFloat_Check(x)) { } else if (PyFloat_Check(x)) {

View File

@ -21,7 +21,7 @@ _runPythonDebug(char* code)
if (py_code == NULL) { if (py_code == NULL) {
fprintf(stderr, "runPythonDebug -- error occurred converting argument:\n"); fprintf(stderr, "runPythonDebug -- error occurred converting argument:\n");
PyErr_Print(); PyErr_Print();
return Js_UNDEFINED; return Js_undefined;
} }
PyObject* result = _PyObject_CallMethodIdObjArgs( PyObject* result = _PyObject_CallMethodIdObjArgs(
@ -30,7 +30,7 @@ _runPythonDebug(char* code)
if (result == NULL) { if (result == NULL) {
fprintf(stderr, "runPythonDebug -- error occurred\n"); fprintf(stderr, "runPythonDebug -- error occurred\n");
PyErr_Print(); PyErr_Print();
return Js_UNDEFINED; return Js_undefined;
} }
printf("runPythonDebug -- eval_code succeeded, it returned:\n"); printf("runPythonDebug -- eval_code succeeded, it returned:\n");