diff --git a/src/ecmascript/mujs.cpp b/src/ecmascript/mujs.cpp index b3405e2e0..801cdbf86 100644 --- a/src/ecmascript/mujs.cpp +++ b/src/ecmascript/mujs.cpp @@ -330,6 +330,28 @@ mujs_eval_boolback(struct ecmascript_interpreter *interpreter, #endif } +void +addmethod(js_State *J, const char *name, js_CFunction fun, int n) +{ + const char *realname = strchr(name, '.'); + realname = realname ? realname + 1 : name; + js_newcfunction(J, fun, name, n); + js_defproperty(J, -2, realname, JS_READONLY | JS_DONTENUM | JS_DONTCONF); +} + +void addproperty(js_State *J, const char *name, js_CFunction getfun, js_CFunction setfun) +{ + const char *realname = strchr(name, '.'); + realname = realname ? realname + 1 : name; + js_newcfunction(J, getfun, name, 0); + if (setfun) { + js_newcfunction(J, setfun, name, 1); + } else { + js_pushnull(J); + } + js_defaccessor(J, -3, realname, JS_READONLY | JS_DONTENUM | JS_DONTCONF); +} + struct module mujs_module = struct_module( /* name: */ N_("mujs"), /* options: */ NULL, diff --git a/src/ecmascript/mujs.h b/src/ecmascript/mujs.h index 8a3407c14..1d0b5f7a4 100644 --- a/src/ecmascript/mujs.h +++ b/src/ecmascript/mujs.h @@ -41,5 +41,8 @@ int mujs_eval_boolback(struct ecmascript_interpreter *interpreter, struct string //void mujs_call_function(struct ecmascript_interpreter *interpreter, JSValueConst fun, struct string *ret); +void addmethod(js_State *J, const char *name, js_CFunction fun, int n); +void addproperty(js_State *J, const char *name, js_CFunction getfun, js_CFunction setfun); + extern struct module mujs_module; #endif diff --git a/src/ecmascript/mujs/console.cpp b/src/ecmascript/mujs/console.cpp index 5c58691cd..27516ab20 100644 --- a/src/ecmascript/mujs/console.cpp +++ b/src/ecmascript/mujs/console.cpp @@ -91,14 +91,9 @@ mjs_console_init(js_State *J) { js_newobject(J); { - js_newcfunction(J, mjs_console_log, "console.log", 1); - js_defproperty(J, -2, "log", JS_DONTENUM); - - js_newcfunction(J, mjs_console_error, "console.error", 1); - js_defproperty(J, -2, "error", JS_DONTENUM); - - js_newcfunction(J, mjs_console_toString, "console.toString", 0); - js_defproperty(J, -2, "toString", JS_DONTENUM); + addmethod(J, "console.log", mjs_console_log, 1); + addmethod(J, "console.error", mjs_console_error, 1); + addmethod(J, "console.toString", mjs_console_toString, 0); } js_defglobal(J, "console", JS_DONTENUM); diff --git a/src/ecmascript/mujs/history.cpp b/src/ecmascript/mujs/history.cpp index a5a6d303b..c93a742c8 100644 --- a/src/ecmascript/mujs/history.cpp +++ b/src/ecmascript/mujs/history.cpp @@ -124,17 +124,10 @@ mjs_history_init(js_State *J) { js_newobject(J); { - js_newcfunction(J, mjs_history_back, "history.back", 0); - js_defproperty(J, -2, "back", JS_DONTENUM); - - js_newcfunction(J, mjs_history_forward, "history.forward", 0); - js_defproperty(J, -2, "forward", JS_DONTENUM); - - js_newcfunction(J, mjs_history_go, "history.go", 1); - js_defproperty(J, -2, "go", JS_DONTENUM); - - js_newcfunction(J, mjs_history_toString, "history.toString", 0); - js_defproperty(J, -2, "toString", JS_DONTENUM); + addmethod(J, "history.back", mjs_history_back, 0); + addmethod(J, "history.forward", mjs_history_forward, 0); + addmethod(J, "history.go", mjs_history_go, 1); + addmethod(J, "history.toString", mjs_history_toString, 0); } js_defglobal(J, "history", JS_DONTENUM); diff --git a/src/ecmascript/mujs/localstorage.cpp b/src/ecmascript/mujs/localstorage.cpp index 90413a91c..c3c64d474 100644 --- a/src/ecmascript/mujs/localstorage.cpp +++ b/src/ecmascript/mujs/localstorage.cpp @@ -174,17 +174,10 @@ mjs_localstorage_init(js_State *J) { js_newobject(J); { - js_newcfunction(J, mjs_localstorage_getitem, "localStorage.getItem", 1); - js_defproperty(J, -2, "getItem", JS_DONTENUM); - - js_newcfunction(J, mjs_localstorage_removeitem, "localStorage.removeItem", 1); - js_defproperty(J, -2, "removeItem", JS_DONTENUM); - - js_newcfunction(J, mjs_localstorage_setitem, "localStorage.setItem", 2); - js_defproperty(J, -2, "setItem", JS_DONTENUM); - - js_newcfunction(J, mjs_localstorage_toString, "localStorage.toString", 0); - js_defproperty(J, -2, "toString", JS_DONTENUM); + addmethod(J, "localStorage.getItem", mjs_localstorage_getitem, 1); + addmethod(J, "localStorage.removeItem", mjs_localstorage_removeitem, 1); + addmethod(J, "localStorage.setItem", mjs_localstorage_setitem, 2); + addmethod(J, "localStorage.toString", mjs_localstorage_toString, 0); } js_defglobal(J, "localStorage", JS_DONTENUM); diff --git a/src/ecmascript/mujs/navigator.cpp b/src/ecmascript/mujs/navigator.cpp index 6a6de2efc..3744afea2 100644 --- a/src/ecmascript/mujs/navigator.cpp +++ b/src/ecmascript/mujs/navigator.cpp @@ -145,8 +145,7 @@ mjs_navigator_init(js_State *J) { js_newobject(J); { - js_newcfunction(J, mjs_navigator_toString, "navigator.toString", 0); - js_defproperty(J, -2, "toString", JS_DONTENUM); + addmethod(J, "navigator.toString", mjs_navigator_toString, 0); js_newcfunction(J, mjs_navigator_get_property_appCodeName, "navigator.appCodeName", 0); js_pushnull(J); diff --git a/src/ecmascript/mujs/screen.cpp b/src/ecmascript/mujs/screen.cpp index 2a7484a22..b7ba15c57 100644 --- a/src/ecmascript/mujs/screen.cpp +++ b/src/ecmascript/mujs/screen.cpp @@ -149,8 +149,7 @@ mjs_screen_init(js_State *J) { js_newobject(J); { - js_newcfunction(J, mjs_screen_toString, "screen.toString", 0); - js_defproperty(J, -2, "toString", JS_DONTENUM); + addmethod(J, "screen.toString", mjs_screen_toString, 0); js_newcfunction(J, mjs_screen_get_property_availHeight, "screen.availHeight", 0); js_pushnull(J); diff --git a/src/ecmascript/mujs/unibar.cpp b/src/ecmascript/mujs/unibar.cpp index 8bb0c2ca4..ffaee8cc5 100644 --- a/src/ecmascript/mujs/unibar.cpp +++ b/src/ecmascript/mujs/unibar.cpp @@ -151,8 +151,7 @@ mjs_menubar_init(js_State *J) { js_newobject(J); { - js_newcfunction(J, mjs_menubar_toString, "menubar.toString", 0); - js_defproperty(J, -2, "toString", JS_DONTENUM); + addmethod(J, "menubar.toString", mjs_menubar_toString, 0); js_newcfunction(J, mjs_menubar_get_property_visible, "menubar.visible", 0); js_newcfunction(J, mjs_menubar_set_property_visible, "menubar.visible", 1); @@ -166,8 +165,7 @@ mjs_statusbar_init(js_State *J) { js_newobject(J); { - js_newcfunction(J, mjs_statusbar_toString, "statusbar.toString", 0); - js_defproperty(J, -2, "toString", JS_DONTENUM); + addmethod(J, "statusbar.toString", mjs_statusbar_toString, 0); js_newcfunction(J, mjs_statusbar_get_property_visible, "statusbar.visible", 0); js_newcfunction(J, mjs_statusbar_set_property_visible, "statusbar.visible", 1); diff --git a/src/ecmascript/mujs/window.cpp b/src/ecmascript/mujs/window.cpp index 0249b0b80..c3cc9d009 100644 --- a/src/ecmascript/mujs/window.cpp +++ b/src/ecmascript/mujs/window.cpp @@ -460,11 +460,8 @@ mjs_window_init(js_State *J) { js_newobject(J); { - js_newcfunction(J, mjs_window_alert, "window.alert", 1); - js_defproperty(J, -2, "alert", JS_DONTENUM); - - js_newcfunction(J, mjs_window_toString, "window.toString", 0); - js_defproperty(J, -2, "toString", JS_DONTENUM); + addmethod(J, "window.alert", mjs_window_alert, 1); + addmethod(J, "window.toString", mjs_window_toString, 0); } js_defglobal(J, "window", JS_DONTENUM);