Skip to content

Commit ffa3de0

Browse files
committed
fix handling of null and undefined return values
1 parent 0f7a5e7 commit ffa3de0

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

dukpy/evaljs.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -86,20 +86,21 @@ def _init_console(self):
8686
self.export_function("dukpy.log.error", lambda *args: log.error(" ".join(args)))
8787
self.export_function("dukpy.log.warn", lambda *args: log.warn(" ".join(args)))
8888
self.evaljs("""
89-
;console = {
90-
log: function() {
91-
call_python('dukpy.log.info', Array.prototype.join.call(arguments, ' '));
92-
},
93-
info: function() {
94-
call_python('dukpy.log.info', Array.prototype.join.call(arguments, ' '));
95-
},
96-
warn: function() {
97-
call_python('dukpy.log.warn', Array.prototype.join.call(arguments, ' '));
98-
},
99-
error: function() {
100-
call_python('dukpy.log.error', Array.prototype.join.call(arguments, ' '));
101-
}
102-
};
89+
;(function() {
90+
globalThis.console = globalThis.console || {};
91+
globalThis.console.log = function() {
92+
globalThis.call_python('dukpy.log.info', Array.prototype.join.call(arguments, ' '));
93+
};
94+
globalThis.console.info = function() {
95+
globalThis.call_python('dukpy.log.info', Array.prototype.join.call(arguments, ' '));
96+
};
97+
globalThis.console.warn = function() {
98+
globalThis.call_python('dukpy.log.warn', Array.prototype.join.call(arguments, ' '));
99+
};
100+
globalThis.console.error = function() {
101+
globalThis.call_python('dukpy.log.error', Array.prototype.join.call(arguments, ' '));
102+
};
103+
})();
103104
""")
104105

105106
def _init_require(self):

src/dukpyjs.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ static PyObject *DukPy_eval_string(PyObject *self, PyObject *args) {
7575
JSValue jsvars;
7676
JSValue json_result;
7777
JSValue global;
78+
int eval_is_null;
79+
int eval_is_undefined;
7880

7981
if (!PyArg_ParseTuple(args, "Oy#y#", &interpreter, &command, &command_len,
8082
&vars, &vars_len))
@@ -141,22 +143,27 @@ static PyObject *DukPy_eval_string(PyObject *self, PyObject *args) {
141143
return raise_js_exception(ctx);
142144
}
143145

146+
eval_is_null = JS_IsNull(eval_result);
147+
eval_is_undefined = JS_IsUndefined(eval_result);
148+
if (eval_is_null || eval_is_undefined) {
149+
JS_FreeValue(ctx, eval_result);
150+
result = Py_BuildValue("y", "{}");
151+
Py_XDECREF(pyctx);
152+
return result;
153+
}
154+
144155
json_result = JS_JSONStringify(ctx, eval_result, JS_UNDEFINED, JS_UNDEFINED);
145156
JS_FreeValue(ctx, eval_result);
146157
if (JS_IsException(json_result)) {
147158
Py_XDECREF(pyctx);
148159
return raise_js_exception(ctx);
149160
}
150161

151-
if (JS_IsNull(json_result)) {
152-
JS_FreeValue(ctx, json_result);
153-
json_result = JS_NewString(ctx, "{}");
154-
}
155-
156162
if (JS_IsUndefined(json_result)) {
157163
JS_FreeValue(ctx, json_result);
158-
PyErr_SetString(DukPyError, "Invalid Result Value");
164+
JS_FreeValue(ctx, eval_result);
159165
Py_XDECREF(pyctx);
166+
PyErr_SetString(DukPyError, "Invalid Result Value");
160167
return NULL;
161168
}
162169

0 commit comments

Comments
 (0)