diff --git a/src/ecmascript/quickjs.h b/src/ecmascript/quickjs.h index a10115a4..8c806fe8 100644 --- a/src/ecmascript/quickjs.h +++ b/src/ecmascript/quickjs.h @@ -3,6 +3,18 @@ #include +#ifdef ECMASCRIPT_DEBUG + +#define RETURN_JS(obj) \ + fprintf(stderr, "%s:%d obj=%p\n", __FILE__, __LINE__, JS_VALUE_GET_PTR(obj)); \ + return obj + +#else + +#define RETURN_JS(obj) return obj + +#endif + struct ecmascript_interpreter; struct form_view; struct form_state; diff --git a/src/ecmascript/quickjs/attr.c b/src/ecmascript/quickjs/attr.c index 779f9042..fd427dce 100644 --- a/src/ecmascript/quickjs/attr.c +++ b/src/ecmascript/quickjs/attr.c @@ -20,6 +20,7 @@ #include "document/forms.h" #include "document/view.h" #include "ecmascript/ecmascript.h" +#include "ecmascript/quickjs.h" #include "ecmascript/quickjs/attr.h" #include "intl/libintl.h" #include "main/select.h" @@ -79,7 +80,9 @@ js_attr_get_property_name(JSContext *ctx, JSValueConst this_val) xmlpp::ustring v = attr->get_name(); - return JS_NewString(ctx, v.c_str()); + JSValue r = JS_NewString(ctx, v.c_str()); + + RETURN_JS(r); } static JSValue @@ -106,7 +109,9 @@ js_attr_get_property_value(JSContext *ctx, JSValueConst this_val) xmlpp::ustring v = attr->get_value(); - return JS_NewString(ctx, v.c_str()); + JSValue r = JS_NewString(ctx, v.c_str()); + + RETURN_JS(r); } static const JSCFunctionListEntry js_attr_proto_funcs[] = { @@ -136,7 +141,7 @@ js_attr_ctor(JSContext *ctx, JSValueConst new_target, int argc, JSValueConst *ar if (JS_IsException(obj)) { goto fail; } - return obj; + RETURN_JS(obj); fail: JS_FreeValue(ctx, obj); @@ -175,5 +180,5 @@ getAttr(JSContext *ctx, void *node) JS_SetOpaque(attr_obj, node); - return attr_obj; + RETURN_JS(attr_obj); } diff --git a/src/ecmascript/quickjs/attributes.c b/src/ecmascript/quickjs/attributes.c index 02676a76..029f55d4 100644 --- a/src/ecmascript/quickjs/attributes.c +++ b/src/ecmascript/quickjs/attributes.c @@ -20,6 +20,7 @@ #include "document/forms.h" #include "document/view.h" #include "ecmascript/ecmascript.h" +#include "ecmascript/quickjs.h" #include "ecmascript/quickjs/attr.h" #include "ecmascript/quickjs/attributes.h" #include "intl/libintl.h" @@ -190,7 +191,7 @@ js_attributes_namedItem2(JSContext *ctx, JSValueConst this_val, const char *str) if (name == attr->get_name()) { JSValue obj = getAttr(ctx, attr); - return obj; + RETURN_JS(obj); } } @@ -219,7 +220,7 @@ js_attributes_getNamedItem(JSContext *ctx, JSValueConst this_val, int argc, JSVa JSValue ret = js_attributes_namedItem2(ctx, this_val, str); JS_FreeCString(ctx, str); - return ret; + RETURN_JS(ret); } static const JSCFunctionListEntry js_attributes_proto_funcs[] = { @@ -250,7 +251,7 @@ js_attributes_ctor(JSContext *ctx, JSValueConst new_target, int argc, JSValueCon if (JS_IsException(obj)) { goto fail; } - return obj; + RETURN_JS(obj); fail: JS_FreeValue(ctx, obj); @@ -290,5 +291,5 @@ getAttributes(JSContext *ctx, void *node) JS_SetOpaque(attributes_obj, node); js_attributes_set_items(ctx, attributes_obj, node); - return attributes_obj; + RETURN_JS(attributes_obj); } diff --git a/src/ecmascript/quickjs/collection.c b/src/ecmascript/quickjs/collection.c index e4d2856d..04293bf0 100644 --- a/src/ecmascript/quickjs/collection.c +++ b/src/ecmascript/quickjs/collection.c @@ -187,7 +187,7 @@ js_htmlCollection_namedItem(JSContext *ctx, JSValueConst this_val, int argc, JSV JSValue ret = js_htmlCollection_namedItem2(ctx, this_val, str); JS_FreeCString(ctx, str); - return ret; + RETURN_JS(ret); } static void @@ -269,7 +269,7 @@ js_htmlCollection_ctor(JSContext *ctx, JSValueConst new_target, int argc, JSValu if (JS_IsException(obj)) { goto fail; } - return obj; + RETURN_JS(obj); fail: JS_FreeValue(ctx, obj); @@ -308,7 +308,8 @@ getCollection(JSContext *ctx, void *node) auto node_find = map_collections.find(node); if (node_find != map_collections.end()) { - return JS_DupValue(ctx, node_find->second); + JSValue r = JS_DupValue(ctx, node_find->second); + RETURN_JS(r); } JSValue htmlCollection_obj = JS_NewArray(ctx); JS_SetPropertyFunctionList(ctx, htmlCollection_obj, js_htmlCollection_proto_funcs, countof(js_htmlCollection_proto_funcs)); @@ -317,5 +318,6 @@ getCollection(JSContext *ctx, void *node) js_htmlCollection_set_items(ctx, htmlCollection_obj, node); map_collections[node] = htmlCollection_obj; - return JS_DupValue(ctx, htmlCollection_obj); + JSValue rr = JS_DupValue(ctx, htmlCollection_obj); + RETURN_JS(rr); } diff --git a/src/ecmascript/quickjs/console.c b/src/ecmascript/quickjs/console.c index 7192560c..a41a1110 100644 --- a/src/ecmascript/quickjs/console.c +++ b/src/ecmascript/quickjs/console.c @@ -16,6 +16,7 @@ #include "dialogs/menu.h" #include "dialogs/status.h" #include "ecmascript/ecmascript.h" +#include "ecmascript/quickjs.h" #include "ecmascript/quickjs/console.h" #include "intl/libintl.h" #include "osdep/newwin.h" @@ -113,7 +114,7 @@ js_console_ctor(JSContext *ctx, JSValueConst new_target, int argc, JSValueConst if (JS_IsException(obj)) { goto fail; } - return obj; + RETURN_JS(obj); fail: JS_FreeValue(ctx, obj); diff --git a/src/ecmascript/quickjs/document.c b/src/ecmascript/quickjs/document.c index 84003f51..06f8b998 100644 --- a/src/ecmascript/quickjs/document.c +++ b/src/ecmascript/quickjs/document.c @@ -134,7 +134,7 @@ js_document_get_property_baseURI(JSContext *ctx, JSValueConst this_val) JSValue ret = JS_NewString(ctx, str); mem_free(str); - return ret; + RETURN_JS(ret); } static JSValue @@ -206,9 +206,11 @@ js_document_get_property_cookie(JSContext *ctx, JSValueConst this_val) strncpy(cookiestr, cookies->source, 1023); done_string(cookies); - return JS_NewString(ctx, cookiestr); + JSValue r = JS_NewString(ctx, cookiestr); + RETURN_JS(r); } else { - return JS_NewStringLen(ctx, "", 0); + JSValue rr = JS_NewStringLen(ctx, "", 0); + RETURN_JS(rr); } } @@ -269,7 +271,8 @@ js_document_get_property_charset(JSContext *ctx, JSValueConst this_val, JSValue encoding = "utf-8"; } - return JS_NewStringLen(ctx, encoding.c_str(), encoding.length()); + JSValue r = JS_NewStringLen(ctx, encoding.c_str(), encoding.length()); + RETURN_JS(r); } static JSValue @@ -406,7 +409,7 @@ js_document_get_property_documentURI(JSContext *ctx, JSValueConst this_val) JSValue ret = JS_NewString(ctx, str); mem_free(str); - return ret; + RETURN_JS(ret); } static JSValue @@ -438,7 +441,7 @@ js_document_get_property_domain(JSContext *ctx, JSValueConst this_val) JSValue ret = JS_NewString(ctx, str); mem_free(str); - return ret; + RETURN_JS(ret); } static JSValue @@ -613,7 +616,7 @@ js_document_get_property_location(JSContext *ctx, JSValueConst this_val) #endif struct ecmascript_interpreter *interpreter = JS_GetContextOpaque(ctx); - return interpreter->location_obj; + RETURN_JS(interpreter->location_obj); } static JSValue @@ -698,7 +701,7 @@ js_document_get_property_referrer(JSContext *ctx, JSValueConst this_val) JSValue ret = JS_NewString(ctx, str); mem_free(str); - return ret; + RETURN_JS(ret); } else { return JS_UNDEFINED; } @@ -712,7 +715,7 @@ js_document_get_property_referrer(JSContext *ctx, JSValueConst this_val) JSValue ret = JS_NewString(ctx, str); mem_free(str); - return ret; + RETURN_JS(ret); } else { return JS_UNDEFINED; } @@ -780,7 +783,8 @@ js_document_get_property_title(JSContext *ctx, JSValueConst this_val) } doc_view = vs->doc_view; document = doc_view->document; - return JS_NewString(ctx, document->title); + JSValue r = JS_NewString(ctx, document->title); + RETURN_JS(r); } static JSValue @@ -848,7 +852,7 @@ js_document_get_property_url(JSContext *ctx, JSValueConst this_val) JSValue ret = JS_NewString(ctx, str); mem_free(str); - return ret; + RETURN_JS(ret); } else { return JS_UNDEFINED; } @@ -1554,7 +1558,8 @@ js_doctype_get_property_publicId(JSContext *ctx, JSValueConst this_val) } xmlpp::ustring v = dtd->get_external_id(); - return JS_NewStringLen(ctx, v.c_str(), v.length()); + JSValue r = JS_NewStringLen(ctx, v.c_str(), v.length()); + RETURN_JS(r); } static JSValue @@ -1571,7 +1576,8 @@ js_doctype_get_property_systemId(JSContext *ctx, JSValueConst this_val) } xmlpp::ustring v = dtd->get_system_id(); - return JS_NewStringLen(ctx, v.c_str(), v.length()); + JSValue r = JS_NewStringLen(ctx, v.c_str(), v.length()); + RETURN_JS(r); } static const JSCFunctionListEntry js_document_proto_funcs[] = { @@ -1638,7 +1644,7 @@ js_document_ctor(JSContext *ctx, JSValueConst new_target, int argc, JSValueConst if (JS_IsException(obj)) { goto fail; } - return obj; + RETURN_JS(obj); fail: JS_FreeValue(ctx, obj); @@ -1664,7 +1670,7 @@ js_document_init(JSContext *ctx, JSValue global_obj) JS_SetPropertyStr(ctx, global_obj, "document", document_proto); - return document_proto; + RETURN_JS(document_proto); } static const JSCFunctionListEntry js_doctype_proto_funcs[] = { @@ -1706,7 +1712,7 @@ js_doctype_ctor(JSContext *ctx, JSValueConst new_target, int argc, JSValueConst if (JS_IsException(obj)) { goto fail; } - return obj; + RETURN_JS(obj); fail: JS_FreeValue(ctx, obj); @@ -1751,7 +1757,8 @@ getDoctype(JSContext *ctx, void *node) auto node_find = map_doctypes.find(node); if (node_find != map_doctypes.end()) { - return JS_DupValue(ctx, node_find->second); + JSValue r = JS_DupValue(ctx, node_find->second); + RETURN_JS(r); } JSValue doctype_obj = JS_NewObjectClass(ctx, js_doctype_class_id); JS_SetPropertyFunctionList(ctx, doctype_obj, js_doctype_proto_funcs, countof(js_doctype_proto_funcs)); @@ -1760,7 +1767,8 @@ getDoctype(JSContext *ctx, void *node) map_doctypes[node] = doctype_obj; - return JS_DupValue(ctx, doctype_obj); + JSValue rr = JS_DupValue(ctx, doctype_obj); + RETURN_JS(rr); } JSValue @@ -1776,5 +1784,5 @@ getDocument(JSContext *ctx, void *doc) JS_SetClassProto(ctx, js_document_class_id, document_obj); JS_SetOpaque(document_obj, doc); - return document_obj; + RETURN_JS(document_obj); } diff --git a/src/ecmascript/quickjs/element.c b/src/ecmascript/quickjs/element.c index 07e7d280..e4aa287a 100644 --- a/src/ecmascript/quickjs/element.c +++ b/src/ecmascript/quickjs/element.c @@ -21,6 +21,7 @@ #include "document/view.h" #include "ecmascript/css2xpath.h" #include "ecmascript/ecmascript.h" +#include "ecmascript/quickjs.h" #include "ecmascript/quickjs/attr.h" #include "ecmascript/quickjs/attributes.h" #include "ecmascript/quickjs/collection.h" @@ -184,7 +185,8 @@ js_element_get_property_className(JSContext *ctx, JSValueConst this_val) } xmlpp::ustring v = el->get_attribute_value("class"); - return JS_NewStringLen(ctx, v.c_str(), v.length()); + JSValue r = JS_NewStringLen(ctx, v.c_str(), v.length()); + RETURN_JS(r); } static JSValue @@ -204,7 +206,8 @@ js_element_get_property_dir(JSContext *ctx, JSValueConst this_val) if (v != "auto" && v != "ltr" && v != "rtl") { v = ""; } - return JS_NewStringLen(ctx, v.c_str(), v.length()); + JSValue r = JS_NewStringLen(ctx, v.c_str(), v.length()); + RETURN_JS(r); } static JSValue @@ -271,7 +274,8 @@ js_element_get_property_id(JSContext *ctx, JSValueConst this_val) } xmlpp::ustring v = el->get_attribute_value("id"); - return JS_NewStringLen(ctx, v.c_str(), v.length()); + JSValue r = JS_NewStringLen(ctx, v.c_str(), v.length()); + RETURN_JS(r); } static JSValue @@ -287,7 +291,8 @@ js_element_get_property_lang(JSContext *ctx, JSValueConst this_val) } xmlpp::ustring v = el->get_attribute_value("lang"); - return JS_NewStringLen(ctx, v.c_str(), v.length()); + JSValue r = JS_NewStringLen(ctx, v.c_str(), v.length()); + RETURN_JS(r); } static JSValue @@ -381,7 +386,8 @@ js_element_get_property_nodeName(JSContext *ctx, JSValueConst this_val) xmlpp::ustring v; if (!node) { - return JS_NewStringLen(ctx, "", 0); + JSValue r = JS_NewStringLen(ctx, "", 0); + RETURN_JS(r); } auto el = dynamic_cast(node); @@ -399,7 +405,8 @@ js_element_get_property_nodeName(JSContext *ctx, JSValueConst this_val) } } - return JS_NewStringLen(ctx, v.c_str(), v.length()); + JSValue rr = JS_NewStringLen(ctx, v.c_str(), v.length()); + RETURN_JS(rr); } static JSValue @@ -449,7 +456,8 @@ js_element_get_property_nodeValue(JSContext *ctx, JSValueConst this_val) if (el) { xmlpp::ustring v = el->get_value(); - return JS_NewStringLen(ctx, v.c_str(), v.length()); + JSValue r = JS_NewStringLen(ctx, v.c_str(), v.length()); + RETURN_JS(r); } auto el2 = dynamic_cast(node); @@ -457,7 +465,8 @@ js_element_get_property_nodeValue(JSContext *ctx, JSValueConst this_val) if (el2) { xmlpp::ustring v = el2->get_content(); - return JS_NewStringLen(ctx, v.c_str(), v.length()); + JSValue r = JS_NewStringLen(ctx, v.c_str(), v.length()); + RETURN_JS(r); } auto el3 = dynamic_cast(node); @@ -465,7 +474,8 @@ js_element_get_property_nodeValue(JSContext *ctx, JSValueConst this_val) if (el3) { xmlpp::ustring v = el3->get_content(); - return JS_NewStringLen(ctx, v.c_str(), v.length()); + JSValue r = JS_NewStringLen(ctx, v.c_str(), v.length()); + RETURN_JS(r); } return JS_UNDEFINED; @@ -500,7 +510,8 @@ js_element_get_property_ownerDocument(JSContext *ctx, JSValueConst this_val) #endif struct ecmascript_interpreter *interpreter = JS_GetContextOpaque(ctx); - return JS_DupValue(ctx, interpreter->document_obj); + JSValue r = JS_DupValue(ctx, interpreter->document_obj); + RETURN_JS(r); } static JSValue @@ -607,7 +618,8 @@ js_element_get_property_tagName(JSContext *ctx, JSValueConst this_val) xmlpp::ustring v = el->get_name(); std::transform(v.begin(), v.end(), v.begin(), ::toupper); - return JS_NewStringLen(ctx, v.c_str(), v.length()); + JSValue r = JS_NewStringLen(ctx, v.c_str(), v.length()); + RETURN_JS(r); } static JSValue @@ -623,7 +635,8 @@ js_element_get_property_title(JSContext *ctx, JSValueConst this_val) } xmlpp::ustring v = el->get_attribute_value("title"); - return JS_NewStringLen(ctx, v.c_str(), v.length()); + JSValue r = JS_NewStringLen(ctx, v.c_str(), v.length()); + RETURN_JS(r); } static int was_el = 0; @@ -728,7 +741,7 @@ js_element_get_property_innerHtml(JSContext *ctx, JSValueConst this_val) JSValue ret = JS_NewStringLen(ctx, buf.source, buf.length); done_string(&buf); - return ret; + RETURN_JS(ret); } static JSValue @@ -748,7 +761,7 @@ js_element_get_property_outerHtml(JSContext *ctx, JSValueConst this_val) JSValue ret = JS_NewStringLen(ctx, buf.source, buf.length); done_string(&buf); - return ret; + RETURN_JS(ret); } static JSValue @@ -768,7 +781,7 @@ js_element_get_property_textContent(JSContext *ctx, JSValueConst this_val) JSValue ret = JS_NewStringLen(ctx, buf.source, buf.length); done_string(&buf); - return ret; + RETURN_JS(ret); } static JSValue @@ -1177,7 +1190,8 @@ js_element_getAttribute(JSContext *ctx, JSValueConst this_val, int argc, JSValue } xmlpp::ustring val = attr->get_value(); - return JS_NewStringLen(ctx, val.c_str(), val.length()); + JSValue r = JS_NewStringLen(ctx, val.c_str(), val.length()); + RETURN_JS(r); } static JSValue @@ -1645,7 +1659,7 @@ js_element_ctor(JSContext *ctx, JSValueConst new_target, int argc, JSValueConst if (JS_IsException(obj)) { goto fail; } - return obj; + RETURN_JS(obj); fail: JS_FreeValue(ctx, obj); @@ -1691,7 +1705,8 @@ getElement(JSContext *ctx, void *node) auto node_find = map_elements.find(node); if (node_find != map_elements.end()) { - return JS_DupValue(ctx, node_find->second); + JSValue r = JS_DupValue(ctx, node_find->second); + RETURN_JS(r); } JSValue element_obj = JS_NewObjectClass(ctx, js_element_class_id); @@ -1702,5 +1717,6 @@ getElement(JSContext *ctx, void *node) map_elements[node] = element_obj; - return JS_DupValue(ctx, element_obj); + JSValue rr = JS_DupValue(ctx, element_obj); + RETURN_JS(rr); } diff --git a/src/ecmascript/quickjs/form.c b/src/ecmascript/quickjs/form.c index 79810b64..40d49a0e 100644 --- a/src/ecmascript/quickjs/form.c +++ b/src/ecmascript/quickjs/form.c @@ -377,7 +377,7 @@ js_form_elements_namedItem(JSContext *ctx, JSValueConst this_val, int argc, JSVa JSValue ret = js_form_elements_namedItem2(ctx, this_val, str); JS_FreeCString(ctx, str); - return ret; + RETURN_JS(ret); } static struct form_view * js_form_get_form_view(JSContext *ctx, JSValueConst this_val, JSValueConst *argv) @@ -413,7 +413,8 @@ js_form_get_property_action(JSContext *ctx, JSValueConst this_val) form = form_GetOpaque(this_val); assert(form); - return JS_NewString(ctx, form->action); + JSValue r = JS_NewString(ctx, form->action); + RETURN_JS(r); } static JSValue @@ -508,7 +509,8 @@ getFormElements(JSContext *ctx, struct form_view *fv) auto node_find = map_form_elements.find(fv); if (node_find != map_form_elements.end()) { - return JS_DupValue(ctx, node_find->second); + JSValue r = JS_DupValue(ctx, node_find->second); + RETURN_JS(r); } JSValue form_elements_obj = JS_NewArray(ctx); @@ -518,7 +520,8 @@ getFormElements(JSContext *ctx, struct form_view *fv) js_form_set_items(ctx, form_elements_obj, fv); map_form_elements[fv] = form_elements_obj; - return JS_DupValue(ctx, form_elements_obj); + JSValue rr = JS_DupValue(ctx, form_elements_obj); + RETURN_JS(rr); } static JSValue @@ -562,14 +565,19 @@ js_form_get_property_encoding(JSContext *ctx, JSValueConst this_val) form = form_GetOpaque(this_val); assert(form); + JSValue r; + switch (form->method) { case FORM_METHOD_GET: case FORM_METHOD_POST: - return JS_NewString(ctx, "application/x-www-form-urlencoded"); + r = JS_NewString(ctx, "application/x-www-form-urlencoded"); + RETURN_JS(r); case FORM_METHOD_POST_MP: - return JS_NewString(ctx, "multipart/form-data"); + r = JS_NewString(ctx, "multipart/form-data"); + RETURN_JS(r); case FORM_METHOD_POST_TEXT_PLAIN: - return JS_NewString(ctx, "text/plain"); + r = JS_NewString(ctx, "text/plain"); + RETURN_JS(r); } return JS_UNDEFINED; @@ -668,14 +676,18 @@ js_form_get_property_method(JSContext *ctx, JSValueConst this_val) form = form_GetOpaque(this_val); assert(form); + JSValue r; + switch (form->method) { case FORM_METHOD_GET: - return JS_NewStringLen(ctx, "GET", 3); + r = JS_NewStringLen(ctx, "GET", 3); + RETURN_JS(r); case FORM_METHOD_POST: case FORM_METHOD_POST_MP: case FORM_METHOD_POST_TEXT_PLAIN: - return JS_NewStringLen(ctx, "POST", 4); + r = JS_NewStringLen(ctx, "POST", 4); + RETURN_JS(r); } return JS_UNDEFINED; @@ -747,7 +759,8 @@ js_form_get_property_name(JSContext *ctx, JSValueConst this_val) form = form_GetOpaque(this_val); assert(form); - return JS_NewString(ctx, form->name); + JSValue r = JS_NewString(ctx, form->name); + RETURN_JS(r); } /* @form_class.setProperty */ @@ -811,7 +824,8 @@ js_form_get_property_target(JSContext *ctx, JSValueConst this_val) form = form_GetOpaque(this_val); assert(form); - return JS_NewString(ctx, form->target); + JSValue r = JS_NewString(ctx, form->target); + RETURN_JS(r); } static JSValue @@ -926,7 +940,7 @@ js_elements_ctor(JSContext *ctx, JSValueConst new_target, int argc, JSValueConst if (JS_IsException(obj)) { goto fail; } - return obj; + RETURN_JS(obj); fail: JS_FreeValue(ctx, obj); @@ -1002,7 +1016,7 @@ js_form_ctor(JSContext *ctx, JSValueConst new_target, int argc, JSValueConst *ar if (JS_IsException(obj)) { goto fail; } - return obj; + RETURN_JS(obj); fail: JS_FreeValue(ctx, obj); @@ -1040,7 +1054,8 @@ getForm(JSContext *ctx, struct form *form) auto node_find = map_form.find(form); if (node_find != map_form.end()) { - return JS_DupValue(ctx, node_find->second); + JSValue r = JS_DupValue(ctx, node_find->second); + RETURN_JS(r); } JSValue form_obj = JS_NewArray(ctx); @@ -1051,5 +1066,6 @@ getForm(JSContext *ctx, struct form *form) map_form[form] = form_obj; - return JS_DupValue(ctx, form_obj); + JSValue rr = JS_DupValue(ctx, form_obj); + RETURN_JS(rr); } diff --git a/src/ecmascript/quickjs/forms.c b/src/ecmascript/quickjs/forms.c index 996a09ac..d22bacb8 100644 --- a/src/ecmascript/quickjs/forms.c +++ b/src/ecmascript/quickjs/forms.c @@ -217,7 +217,7 @@ js_forms_namedItem(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst JSValue ret = js_find_form_by_name(ctx, doc_view, str); JS_FreeCString(ctx, str); - return ret; + RETURN_JS(ret); } #if 0 @@ -323,7 +323,7 @@ js_forms_ctor(JSContext *ctx, JSValueConst new_target, int argc, JSValueConst *a if (JS_IsException(obj)) { goto fail; } - return obj; + RETURN_JS(obj); fail: JS_FreeValue(ctx, obj); @@ -361,7 +361,8 @@ getForms(JSContext *ctx, void *node) auto node_find = map_forms.find(node); if (node_find != map_forms.end()) { - return JS_DupValue(ctx, node_find->second); + JSValue r = JS_DupValue(ctx, node_find->second); + RETURN_JS(r); } JSValue forms_obj = JS_NewArray(ctx); JS_SetPropertyFunctionList(ctx, forms_obj, js_forms_proto_funcs, countof(js_forms_proto_funcs)); @@ -369,5 +370,6 @@ getForms(JSContext *ctx, void *node) js_forms_set_items(ctx, forms_obj, node); map_forms[node] = forms_obj; - return JS_DupValue(ctx, forms_obj); + JSValue rr = JS_DupValue(ctx, forms_obj); + RETURN_JS(rr); } diff --git a/src/ecmascript/quickjs/history.c b/src/ecmascript/quickjs/history.c index a02c453b..945492f7 100644 --- a/src/ecmascript/quickjs/history.c +++ b/src/ecmascript/quickjs/history.c @@ -20,6 +20,7 @@ #include "document/forms.h" #include "document/view.h" #include "ecmascript/ecmascript.h" +#include "ecmascript/quickjs.h" #include "ecmascript/quickjs/history.h" #include "ecmascript/quickjs/window.h" #include "intl/libintl.h" @@ -148,7 +149,7 @@ js_history_ctor(JSContext *ctx, JSValueConst new_target, int argc, JSValueConst if (JS_IsException(obj)) { goto fail; } - return obj; + RETURN_JS(obj); fail: JS_FreeValue(ctx, obj); diff --git a/src/ecmascript/quickjs/implementation.c b/src/ecmascript/quickjs/implementation.c index 89b0758c..abc18ae3 100644 --- a/src/ecmascript/quickjs/implementation.c +++ b/src/ecmascript/quickjs/implementation.c @@ -11,6 +11,7 @@ #include "elinks.h" #include "ecmascript/ecmascript.h" +#include "ecmascript/quickjs.h" #include "ecmascript/quickjs/document.h" #include "ecmascript/quickjs/implementation.h" #include "util/conv.h" @@ -88,7 +89,7 @@ js_implementation_ctor(JSContext *ctx, JSValueConst new_target, int argc, JSValu if (JS_IsException(obj)) { goto fail; } - return obj; + RETURN_JS(obj); fail: JS_FreeValue(ctx, obj); @@ -128,5 +129,5 @@ getImplementation(JSContext *ctx) // JS_SetConstructor(ctx, implementation_class, implementation_obj); JS_SetClassProto(ctx, js_implementation_class_id, implementation_obj); - return implementation_obj; + RETURN_JS(implementation_obj); } diff --git a/src/ecmascript/quickjs/input.c b/src/ecmascript/quickjs/input.c index fbf0c527..6212d632 100644 --- a/src/ecmascript/quickjs/input.c +++ b/src/ecmascript/quickjs/input.c @@ -109,11 +109,13 @@ js_input_get_property_accessKey(JSContext *ctx, JSValueConst this_val) } if (!link->accesskey) { - return JS_NewStringLen(ctx, "", 0); + JSValue r = JS_NewStringLen(ctx, "", 0); + RETURN_JS(r); } else { const char *keystr = encode_utf8(link->accesskey); if (keystr) { - return JS_NewString(ctx, keystr); + JSValue r = JS_NewString(ctx, keystr); + RETURN_JS(r); } else { #ifdef ECMASCRIPT_DEBUG fprintf(stderr, "%s:%s %d\n", __FILE__, __FUNCTION__, __LINE__); @@ -226,7 +228,8 @@ js_input_get_property_alt(JSContext *ctx, JSValueConst this_val) assert(fc); assert(fc->form && fs); - return JS_NewString(ctx, fc->alt); + JSValue r = JS_NewString(ctx, fc->alt); + RETURN_JS(r); } static JSValue @@ -408,7 +411,8 @@ js_input_get_property_defaultValue(JSContext *ctx, JSValueConst this_val) assert(fc->form && fs); /* FIXME (bug 805): convert from the charset of the document */ - return JS_NewString(ctx, fc->default_value); + JSValue r = JS_NewString(ctx, fc->default_value); + RETURN_JS(r); } static JSValue @@ -631,7 +635,8 @@ js_input_get_property_name(JSContext *ctx, JSValueConst this_val) assert(fc); assert(fc->form && fs); - return JS_NewString(ctx, fc->name); + JSValue r = JS_NewString(ctx, fc->name); + RETURN_JS(r); } /* @input_class.setProperty */ @@ -937,7 +942,8 @@ js_input_get_property_src(JSContext *ctx, JSValueConst this_val) if (linknum >= 0) link = &document->links[linknum]; if (link && link->where_img) { - return JS_NewString(ctx, link->where_img); + JSValue r = JS_NewString(ctx, link->where_img); + RETURN_JS(r); } else { return JS_UNDEFINED; } @@ -1100,7 +1106,8 @@ js_input_get_property_type(JSContext *ctx, JSValueConst this_val) default: INTERNAL("input_get_property() upon a non-input item."); break; } - return JS_NewString(ctx, s); + JSValue r = JS_NewString(ctx, s); + RETURN_JS(r); } static JSValue @@ -1119,7 +1126,8 @@ js_input_get_property_value(JSContext *ctx, JSValueConst this_val) return JS_NULL; /* detached */ } - return JS_NewString(ctx, fs->value); + JSValue r = JS_NewString(ctx, fs->value); + RETURN_JS(r); } static JSValue @@ -1403,7 +1411,7 @@ js_input_ctor(JSContext *ctx, JSValueConst new_target, int argc, JSValueConst *a if (JS_IsException(obj)) { goto fail; } - return obj; + RETURN_JS(obj); fail: JS_FreeValue(ctx, obj); @@ -1447,7 +1455,8 @@ getInput(JSContext *ctx, struct form_state *fs) auto node_find = map_inputs.find(fs); if (node_find != map_inputs.end()) { - return JS_DupValue(ctx, node_find->second); + JSValue r = JS_DupValue(ctx, node_find->second); + RETURN_JS(r); } JSValue input_obj = JS_NewObjectClass(ctx, js_input_class_id); @@ -1457,5 +1466,6 @@ getInput(JSContext *ctx, struct form_state *fs) fs->ecmascript_obj = input_obj; map_inputs[fs] = input_obj; - return JS_DupValue(ctx, input_obj); + JSValue rr = JS_DupValue(ctx, input_obj); + RETURN_JS(rr); } diff --git a/src/ecmascript/quickjs/localstorage.c b/src/ecmascript/quickjs/localstorage.c index 843a2f59..4abfbfb4 100644 --- a/src/ecmascript/quickjs/localstorage.c +++ b/src/ecmascript/quickjs/localstorage.c @@ -22,6 +22,7 @@ #include "document/view.h" #include "ecmascript/ecmascript.h" #include "ecmascript/localstorage-db.h" +#include "ecmascript/quickjs.h" #include "ecmascript/quickjs/localstorage.h" #include "intl/libintl.h" #include "main/select.h" @@ -76,7 +77,6 @@ readFromStorage(const unsigned char *key) static void saveToStorage(const unsigned char *key, const unsigned char *val) { - if (local_storage_ready==0) { db_prepare_structure(local_storage_filename); local_storage_ready=1; @@ -100,8 +100,6 @@ js_localstorage_getitem(JSContext *ctx, JSValueConst this_val, int argc, JSValue #ifdef ECMASCRIPT_DEBUG fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); #endif - struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)JS_GetContextOpaque(ctx); - if (argc != 1) { return JS_UNDEFINED; @@ -116,17 +114,16 @@ js_localstorage_getitem(JSContext *ctx, JSValueConst this_val, int argc, JSValue return JS_EXCEPTION; } - if (key) { - unsigned char *val = readFromStorage(key); + unsigned char *val = readFromStorage(key); + JS_FreeCString(ctx, key); - JSValue ret = JS_NewString(ctx, val); - - mem_free(val); - JS_FreeCString(ctx, key); - return ret; + if (!val) { + return JS_NULL; } - return JS_UNDEFINED; + JSValue ret = JS_NewString(ctx, val); + mem_free(val); + RETURN_JS(ret); } /* @localstorage_funcs{"setItem"} */ @@ -197,7 +194,7 @@ js_localstorage_ctor(JSContext *ctx, JSValueConst new_target, int argc, JSValueC if (JS_IsException(obj)) { goto fail; } - return obj; + RETURN_JS(obj); fail: JS_FreeValue(ctx, obj); @@ -207,20 +204,23 @@ fail: int js_localstorage_init(JSContext *ctx, JSValue global_obj) { - JSValue localstorage_proto, localstorage_class; +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + static int initialized; - /* create the localstorage class */ - JS_NewClassID(&js_localstorage_class_id); - JS_NewClass(JS_GetRuntime(ctx), js_localstorage_class_id, &js_localstorage_class); + if (!initialized) { + /* create the localstorage class */ + JS_NewClassID(&js_localstorage_class_id); + JS_NewClass(JS_GetRuntime(ctx), js_localstorage_class_id, &js_localstorage_class); + initialized = 1; + } - localstorage_proto = JS_NewObject(ctx); - JS_SetPropertyFunctionList(ctx, localstorage_proto, js_localstorage_proto_funcs, countof(js_localstorage_proto_funcs)); + JSValue localstorage_obj = JS_NewObjectClass(ctx, js_localstorage_class_id); + JS_SetPropertyFunctionList(ctx, localstorage_obj, js_localstorage_proto_funcs, countof(js_localstorage_proto_funcs)); + JS_SetClassProto(ctx, js_localstorage_class_id, localstorage_obj); - localstorage_class = JS_NewCFunction2(ctx, js_localstorage_ctor, "localStorage", 0, JS_CFUNC_constructor, 0); - /* set proto.constructor and ctor.prototype */ - JS_SetConstructor(ctx, localstorage_class, localstorage_proto); - JS_SetClassProto(ctx, js_localstorage_class_id, localstorage_proto); + JS_SetPropertyStr(ctx, global_obj, "localStorage", localstorage_obj); - JS_SetPropertyStr(ctx, global_obj, "localStorage", localstorage_proto); return 0; } diff --git a/src/ecmascript/quickjs/location.c b/src/ecmascript/quickjs/location.c index d67b8ce1..ccf829a2 100644 --- a/src/ecmascript/quickjs/location.c +++ b/src/ecmascript/quickjs/location.c @@ -20,6 +20,7 @@ #include "document/forms.h" #include "document/view.h" #include "ecmascript/ecmascript.h" +#include "ecmascript/quickjs.h" #include "ecmascript/quickjs/location.h" #include "ecmascript/quickjs/window.h" #include "intl/libintl.h" @@ -72,7 +73,7 @@ js_location_get_property_hash(JSContext *ctx, JSValueConst this_val) JSValue ret = JS_NewStringLen(ctx, fragment.source, fragment.length); done_string(&fragment); - return ret; + RETURN_JS(ret); } static JSValue @@ -103,7 +104,7 @@ js_location_get_property_host(JSContext *ctx, JSValueConst this_val) JSValue ret = JS_NewString(ctx, str); mem_free(str); - return ret; + RETURN_JS(ret); } static JSValue @@ -134,7 +135,7 @@ js_location_get_property_hostname(JSContext *ctx, JSValueConst this_val) JSValue ret = JS_NewString(ctx, str); mem_free(str); - return ret; + RETURN_JS(ret); } static JSValue @@ -165,7 +166,7 @@ js_location_get_property_href(JSContext *ctx, JSValueConst this_val) JSValue ret = JS_NewString(ctx, str); mem_free(str); - return ret; + RETURN_JS(ret); } static JSValue @@ -196,7 +197,7 @@ js_location_get_property_origin(JSContext *ctx, JSValueConst this_val) JSValue ret = JS_NewString(ctx, str); mem_free(str); - return ret; + RETURN_JS(ret); } static JSValue @@ -226,7 +227,7 @@ js_location_get_property_pathname(JSContext *ctx, JSValueConst this_val) JSValue ret = JS_NewStringLen(ctx, pathname.source, pathname.length); done_string(&pathname); - return ret; + RETURN_JS(ret); } static JSValue @@ -254,7 +255,7 @@ js_location_get_property_port(JSContext *ctx, JSValueConst this_val) JSValue ret = JS_NewStringLen(ctx, port.source, port.length); done_string(&port); - return ret; + RETURN_JS(ret); } static JSValue @@ -287,7 +288,7 @@ js_location_get_property_protocol(JSContext *ctx, JSValueConst this_val) JSValue ret = JS_NewStringLen(ctx, proto.source, proto.length); done_string(&proto); - return ret; + RETURN_JS(ret); } static JSValue @@ -318,7 +319,7 @@ js_location_get_property_search(JSContext *ctx, JSValueConst this_val) JSValue ret = JS_NewStringLen(ctx, search.source, search.length); done_string(&search); - return ret; + RETURN_JS(ret); } static JSValue @@ -613,7 +614,7 @@ js_location_ctor(JSContext *ctx, JSValueConst new_target, int argc, JSValueConst if (JS_IsException(obj)) { goto fail; } - return obj; + RETURN_JS(obj); fail: JS_FreeValue(ctx, obj); @@ -638,5 +639,5 @@ js_location_init(JSContext *ctx, JSValue global_obj) JS_SetClassProto(ctx, js_location_class_id, location_proto); JS_SetPropertyStr(ctx, global_obj, "location", location_proto); - return location_proto; + RETURN_JS(location_proto); } diff --git a/src/ecmascript/quickjs/navigator.c b/src/ecmascript/quickjs/navigator.c index 74d73348..41fe94c2 100644 --- a/src/ecmascript/quickjs/navigator.c +++ b/src/ecmascript/quickjs/navigator.c @@ -20,6 +20,7 @@ #include "document/forms.h" #include "document/view.h" #include "ecmascript/ecmascript.h" +#include "ecmascript/quickjs.h" #include "ecmascript/quickjs/navigator.h" #include "intl/libintl.h" #include "main/select.h" @@ -53,7 +54,8 @@ js_navigator_get_property_appCodeName(JSContext *ctx, JSValueConst this_val) #ifdef ECMASCRIPT_DEBUG fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); #endif - return JS_NewString(ctx, "Mozilla"); /* More like a constant nowadays. */ + JSValue r = JS_NewString(ctx, "Mozilla"); /* More like a constant nowadays. */ + RETURN_JS(r); } static JSValue @@ -62,7 +64,8 @@ js_navigator_get_property_appName(JSContext *ctx, JSValueConst this_val) #ifdef ECMASCRIPT_DEBUG fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); #endif - return JS_NewString(ctx, "ELinks (roughly compatible with Netscape Navigator, Mozilla and Microsoft Internet Explorer)"); + JSValue r = JS_NewString(ctx, "ELinks (roughly compatible with Netscape Navigator, Mozilla and Microsoft Internet Explorer)"); + RETURN_JS(r); } static JSValue @@ -71,7 +74,8 @@ js_navigator_get_property_appVersion(JSContext *ctx, JSValueConst this_val) #ifdef ECMASCRIPT_DEBUG fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); #endif - return JS_NewString(ctx, VERSION); + JSValue r = JS_NewString(ctx, VERSION); + RETURN_JS(r); } static JSValue @@ -82,7 +86,8 @@ js_navigator_get_property_language(JSContext *ctx, JSValueConst this_val) #endif #ifdef CONFIG_NLS if (get_opt_bool("protocol.http.accept_ui_language", NULL)) { - return JS_NewString(ctx, language_to_iso639(current_language)); + JSValue r = JS_NewString(ctx, language_to_iso639(current_language)); + RETURN_JS(r); } #endif return JS_UNDEFINED; @@ -94,7 +99,8 @@ js_navigator_get_property_platform(JSContext *ctx, JSValueConst this_val) #ifdef ECMASCRIPT_DEBUG fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); #endif - return JS_NewString(ctx, system_name); + JSValue r = JS_NewString(ctx, system_name); + RETURN_JS(r); } static JSValue @@ -127,10 +133,12 @@ js_navigator_get_property_userAgent(JSContext *ctx, JSValueConst this_val) safe_strncpy(custr, ustr, 256); mem_free(ustr); - return JS_NewString(ctx, custr); + JSValue r = JS_NewString(ctx, custr); + RETURN_JS(r); } } - return JS_NewString(ctx, system_name); + JSValue rr = JS_NewString(ctx, system_name); + RETURN_JS(rr); } static const JSCFunctionListEntry js_navigator_proto_funcs[] = { @@ -164,7 +172,7 @@ js_navigator_ctor(JSContext *ctx, JSValueConst new_target, int argc, JSValueCons if (JS_IsException(obj)) { goto fail; } - return obj; + RETURN_JS(obj); fail: JS_FreeValue(ctx, obj); diff --git a/src/ecmascript/quickjs/nodelist.c b/src/ecmascript/quickjs/nodelist.c index 1f34cf97..22a865c5 100644 --- a/src/ecmascript/quickjs/nodelist.c +++ b/src/ecmascript/quickjs/nodelist.c @@ -20,6 +20,7 @@ #include "document/forms.h" #include "document/view.h" #include "ecmascript/ecmascript.h" +#include "ecmascript/quickjs.h" #include "ecmascript/quickjs/element.h" #include "ecmascript/quickjs/nodelist.h" #include "ecmascript/quickjs/window.h" @@ -171,7 +172,7 @@ js_nodeList_ctor(JSContext *ctx, JSValueConst new_target, int argc, JSValueConst if (JS_IsException(obj)) { goto fail; } - return obj; + RETURN_JS(obj); fail: JS_FreeValue(ctx, obj); @@ -214,6 +215,5 @@ getNodeList(JSContext *ctx, void *node) JS_SetOpaque(nodeList_obj, node); js_nodeList_set_items(ctx, nodeList_obj, node); - return nodeList_obj; + RETURN_JS(nodeList_obj); } - diff --git a/src/ecmascript/quickjs/screen.c b/src/ecmascript/quickjs/screen.c index c38c2fe3..0268ea0e 100644 --- a/src/ecmascript/quickjs/screen.c +++ b/src/ecmascript/quickjs/screen.c @@ -20,6 +20,7 @@ #include "document/forms.h" #include "document/view.h" #include "ecmascript/ecmascript.h" +#include "ecmascript/quickjs.h" #include "ecmascript/quickjs/screen.h" #include "ecmascript/quickjs/window.h" #include "intl/libintl.h" @@ -161,7 +162,8 @@ js_screen_ctor(JSContext *ctx, JSValueConst new_target, int argc, JSValueConst * if (JS_IsException(obj)) { goto fail; } - return obj; + + RETURN_JS(obj); fail: JS_FreeValue(ctx, obj); diff --git a/src/ecmascript/quickjs/unibar.c b/src/ecmascript/quickjs/unibar.c index 56cf09c4..3d321a34 100644 --- a/src/ecmascript/quickjs/unibar.c +++ b/src/ecmascript/quickjs/unibar.c @@ -20,6 +20,7 @@ #include "document/forms.h" #include "document/view.h" #include "ecmascript/ecmascript.h" +#include "ecmascript/quickjs.h" #include "ecmascript/quickjs/unibar.h" #include "ecmascript/quickjs/window.h" #include "intl/libintl.h" @@ -143,7 +144,7 @@ js_menubar_ctor(JSContext *ctx, JSValueConst new_target, int argc, JSValueConst if (JS_IsException(obj)) { goto fail; } - return obj; + RETURN_JS(obj); fail: JS_FreeValue(ctx, obj); @@ -168,7 +169,7 @@ js_statusbar_ctor(JSContext *ctx, JSValueConst new_target, int argc, JSValueCons if (JS_IsException(obj)) { goto fail; } - return obj; + RETURN_JS(obj); fail: JS_FreeValue(ctx, obj); diff --git a/src/ecmascript/quickjs/window.c b/src/ecmascript/quickjs/window.c index 5949adc2..f1156bda 100644 --- a/src/ecmascript/quickjs/window.c +++ b/src/ecmascript/quickjs/window.c @@ -20,6 +20,7 @@ #include "document/forms.h" #include "document/view.h" #include "ecmascript/ecmascript.h" +#include "ecmascript/quickjs.h" #include "ecmascript/quickjs/window.h" #include "intl/libintl.h" #include "main/select.h" @@ -254,7 +255,8 @@ js_window_get_property_self(JSContext *ctx, JSValueConst this_val) #ifdef ECMASCRIPT_DEBUG fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); #endif - return JS_DupValue(ctx, this_val); + JSValue r = JS_DupValue(ctx, this_val); + RETURN_JS(r); } static JSValue @@ -405,7 +407,7 @@ js_window_ctor(JSContext *ctx, JSValueConst new_target, int argc, JSValueConst * if (JS_IsException(obj)) { goto fail; } - return obj; + RETURN_JS(obj); fail: JS_FreeValue(ctx, obj);