diff options
author | Daniel Silverstone <dsilvers@digital-scurf.org> | 2015-07-19 12:36:39 +0100 |
---|---|---|
committer | Daniel Silverstone <dsilvers@digital-scurf.org> | 2015-07-19 12:36:39 +0100 |
commit | 10c5dcd078700ff574512fc692adb6835653ba38 (patch) | |
tree | 94828bb4a1a14e3a9c78d2d9a45c25e902330161 /javascript/duktape | |
parent | d68e2c879c9254cae1ea8e248ebe70ddf5e797e6 (diff) | |
download | netsurf-10c5dcd078700ff574512fc692adb6835653ba38.tar.gz netsurf-10c5dcd078700ff574512fc692adb6835653ba38.tar.bz2 |
REWORK: ALL THIS CRUD
Diffstat (limited to 'javascript/duktape')
-rw-r--r-- | javascript/duktape/character_data.c | 50 | ||||
-rw-r--r-- | javascript/duktape/comment.c | 50 | ||||
-rw-r--r-- | javascript/duktape/document.c | 60 | ||||
-rw-r--r-- | javascript/duktape/node.c | 20 | ||||
-rw-r--r-- | javascript/duktape/private.h | 15 | ||||
-rw-r--r-- | javascript/duktape/prototypes.h | 3 | ||||
-rw-r--r-- | javascript/duktape/text.c | 50 | ||||
-rw-r--r-- | javascript/duktape/window.c | 21 |
8 files changed, 253 insertions, 16 deletions
diff --git a/javascript/duktape/character_data.c b/javascript/duktape/character_data.c new file mode 100644 index 000000000..a71de478f --- /dev/null +++ b/javascript/duktape/character_data.c @@ -0,0 +1,50 @@ +/* DO NOT USE, DODGY BIT FOR VINCE */ + +#include <dom/dom.h> + +#include "utils/log.h" + +#include "javascript/dukky.h" + +DUKKY_FUNC_INIT(character_data, struct dom_node_character_data *character_data) +{ + DUKKY_FUNC_T(node, __init)(ctx, &priv->parent, (struct dom_node *)character_data); + LOG("Initialise %p (priv=%p)", duk_get_heapptr(ctx, 0), priv); +} + +DUKKY_FUNC_FINI(character_data) +{ + /* do any character_data finalisation here, priv ptr exists */ + LOG("Finalise %p", duk_get_heapptr(ctx, 0)); + DUKKY_FUNC_T(node, __fini)(ctx, &priv->parent); +} + +static DUKKY_FUNC(character_data, __constructor) +{ + DUKKY_CREATE_PRIVATE(character_data); + DUKKY_FUNC_T(character_data, __init)(ctx, priv, + duk_get_pointer(ctx, 1)); + duk_set_top(ctx, 1); + return 1; +} + +static DUKKY_FUNC(character_data, __destructor) +{ + DUKKY_SAFE_GET_PRIVATE(character_data, 0); + DUKKY_FUNC_T(character_data, __fini)(ctx, priv); + free(priv); + return 0; +} + +DUKKY_FUNC(character_data, __proto) +{ + /* Populate character_data's prototypical functionality */ + + /* Set this prototype's prototype (left-parent)*/ + DUKKY_GET_PROTOTYPE(node); + duk_set_prototype(ctx, 0); + /* And the initialiser/finalizer */ + DUKKY_SET_DESTRUCTOR(0, character_data); + DUKKY_SET_CONSTRUCTOR(0, character_data, 1); + return 1; /* The proto object */ +} diff --git a/javascript/duktape/comment.c b/javascript/duktape/comment.c new file mode 100644 index 000000000..877793af2 --- /dev/null +++ b/javascript/duktape/comment.c @@ -0,0 +1,50 @@ +/* DO NOT USE, DODGY BIT FOR VINCE */ + +#include <dom/dom.h> + +#include "utils/log.h" + +#include "javascript/dukky.h" + +DUKKY_FUNC_INIT(comment, struct dom_node_comment *comment) +{ + DUKKY_FUNC_T(character_data, __init)(ctx, &priv->parent, (struct dom_node_character_data *)comment); + LOG("Initialise %p (priv=%p)", duk_get_heapptr(ctx, 0), priv); +} + +DUKKY_FUNC_FINI(comment) +{ + /* do any comment finalisation here, priv ptr exists */ + LOG("Finalise %p", duk_get_heapptr(ctx, 0)); + DUKKY_FUNC_T(character_data, __fini)(ctx, &priv->parent); +} + +static DUKKY_FUNC(comment, __constructor) +{ + DUKKY_CREATE_PRIVATE(comment); + DUKKY_FUNC_T(comment, __init)(ctx, priv, + duk_get_pointer(ctx, 1)); + duk_set_top(ctx, 1); + return 1; +} + +static DUKKY_FUNC(comment, __destructor) +{ + DUKKY_SAFE_GET_PRIVATE(comment, 0); + DUKKY_FUNC_T(comment, __fini)(ctx, priv); + free(priv); + return 0; +} + +DUKKY_FUNC(comment, __proto) +{ + /* Populate comment's prototypical functionality */ + + /* Set this prototype's prototype (left-parent)*/ + DUKKY_GET_PROTOTYPE(character_data); + duk_set_prototype(ctx, 0); + /* And the initialiser/finalizer */ + DUKKY_SET_DESTRUCTOR(0, comment); + DUKKY_SET_CONSTRUCTOR(0, comment, 1); + return 1; /* The proto object */ +} diff --git a/javascript/duktape/document.c b/javascript/duktape/document.c index 07b8becd3..701d2d40d 100644 --- a/javascript/duktape/document.c +++ b/javascript/duktape/document.c @@ -55,10 +55,70 @@ static DUKKY_FUNC(document, write) return 0; } +static DUKKY_FUNC(document, createTextNode) +{ + DUKKY_GET_METHOD_PRIVATE(document); + dom_node *newnode; + dom_exception err; + duk_size_t text_len; + const char *text = duk_safe_to_lstring(ctx, 0, &text_len); + dom_string *text_str; + + err = dom_string_create((const uint8_t*)text, text_len, &text_str); + if (err != DOM_NO_ERR) return 0; /* coerced to undefined */ + + err = dom_document_create_text_node(priv->parent.node, + text_str, + &newnode); + if (err != DOM_NO_ERR) { + dom_string_unref(text_str); + return 0; /* coerced to undefined */ + } + + dom_string_unref(text_str); + + dukky_push_node(ctx, newnode); + + dom_node_unref(newnode); + + return 1; +} + +static DUKKY_GETTER(document, body) +{ + DUKKY_GET_METHOD_PRIVATE(document); + struct dom_nodelist *nodes; + struct dom_node *retnode; + dom_exception err; + err = dom_document_get_elements_by_tag_name(priv->parent.node, + corestring_dom_BODY, + &nodes); + if (err != DOM_NO_ERR) return 0; /* coerced to undefined */ + + err = dom_nodelist_item(nodes, 0, &retnode); + + if (err != DOM_NO_ERR) { + dom_nodelist_unref(nodes); + return 0; /* coerced to undefined */ + } + + dom_nodelist_unref(nodes); + + if (retnode == NULL) return 0; /* coerced to undefined */ + + dukky_push_node(ctx, retnode); + + dom_node_unref(retnode); + + return 1; +} + DUKKY_FUNC(document, __proto) { /* Populate document's prototypical functionality */ DUKKY_ADD_METHOD(document, write, 1); + DUKKY_ADD_METHOD(document, createTextNode, 1); + DUKKY_POPULATE_READONLY_PROPERTY(document, body); /* Set this prototype's prototype (left-parent)*/ DUKKY_GET_PROTOTYPE(node); duk_set_prototype(ctx, 0); diff --git a/javascript/duktape/node.c b/javascript/duktape/node.c index 5a0bc0e1a..d632a2414 100644 --- a/javascript/duktape/node.c +++ b/javascript/duktape/node.c @@ -38,10 +38,28 @@ static DUKKY_FUNC(node, __destructor) return 0; } +static DUKKY_FUNC(node, appendChild) +{ + DUKKY_GET_METHOD_PRIVATE(node); + + if (!dukky_instanceof(ctx, PROTO_NAME(node))) return 0; + + DUKKY_SAFE_GET_ANOTHER(other,node,0); + + dom_exception err; + dom_node *spare; + + err = dom_node_append_child(priv->node, other->node, &spare); + if (err != DOM_NO_ERR) return 0; + dom_node_unref(spare); + + return 0; +} + DUKKY_FUNC(node, __proto) { /* Populate node's prototypical functionality */ - + DUKKY_ADD_METHOD(node, appendChild, 1); /* Set this prototype's prototype (left-parent)*/ DUKKY_GET_PROTOTYPE(event_target); duk_set_prototype(ctx, 0); diff --git a/javascript/duktape/private.h b/javascript/duktape/private.h index 5d5a070c3..6dd3086fd 100644 --- a/javascript/duktape/private.h +++ b/javascript/duktape/private.h @@ -7,6 +7,9 @@ struct dom_node; struct dom_element; struct dom_document; struct dom_html_element; +struct dom_node_character_data; +struct dom_node_text; +struct dom_node_comment; typedef struct { } event_target_private_t; @@ -24,6 +27,18 @@ typedef struct { typedef struct { node_private_t parent; +} character_data_private_t; + +typedef struct { + character_data_private_t parent; +} text_private_t; + +typedef struct { + character_data_private_t parent; +} comment_private_t; + +typedef struct { + node_private_t parent; } element_private_t; typedef struct { diff --git a/javascript/duktape/prototypes.h b/javascript/duktape/prototypes.h index 380b74111..aa1813e2f 100644 --- a/javascript/duktape/prototypes.h +++ b/javascript/duktape/prototypes.h @@ -4,6 +4,9 @@ DUKKY_DECLARE_INTERFACE(event_target); DUKKY_DECLARE_INTERFACE(window, struct browser_window *, struct html_content *); DUKKY_DECLARE_INTERFACE(node, struct dom_node *); +DUKKY_DECLARE_INTERFACE(character_data, struct dom_node_character_data *); +DUKKY_DECLARE_INTERFACE(text, struct dom_node_text *); +DUKKY_DECLARE_INTERFACE(comment, struct dom_node_comment *); DUKKY_DECLARE_INTERFACE(document, struct dom_document *); DUKKY_DECLARE_INTERFACE(element, struct dom_element *); DUKKY_DECLARE_INTERFACE(html_element, struct dom_html_element *); diff --git a/javascript/duktape/text.c b/javascript/duktape/text.c new file mode 100644 index 000000000..7669bae11 --- /dev/null +++ b/javascript/duktape/text.c @@ -0,0 +1,50 @@ +/* DO NOT USE, DODGY BIT FOR VINCE */ + +#include <dom/dom.h> + +#include "utils/log.h" + +#include "javascript/dukky.h" + +DUKKY_FUNC_INIT(text, struct dom_node_text *text) +{ + DUKKY_FUNC_T(character_data, __init)(ctx, &priv->parent, (struct dom_node_character_data *)text); + LOG("Initialise %p (priv=%p)", duk_get_heapptr(ctx, 0), priv); +} + +DUKKY_FUNC_FINI(text) +{ + /* do any text finalisation here, priv ptr exists */ + LOG("Finalise %p", duk_get_heapptr(ctx, 0)); + DUKKY_FUNC_T(character_data, __fini)(ctx, &priv->parent); +} + +static DUKKY_FUNC(text, __constructor) +{ + DUKKY_CREATE_PRIVATE(text); + DUKKY_FUNC_T(text, __init)(ctx, priv, + duk_get_pointer(ctx, 1)); + duk_set_top(ctx, 1); + return 1; +} + +static DUKKY_FUNC(text, __destructor) +{ + DUKKY_SAFE_GET_PRIVATE(text, 0); + DUKKY_FUNC_T(text, __fini)(ctx, priv); + free(priv); + return 0; +} + +DUKKY_FUNC(text, __proto) +{ + /* Populate text's prototypical functionality */ + + /* Set this prototype's prototype (left-parent)*/ + DUKKY_GET_PROTOTYPE(character_data); + duk_set_prototype(ctx, 0); + /* And the initialiser/finalizer */ + DUKKY_SET_DESTRUCTOR(0, text); + DUKKY_SET_CONSTRUCTOR(0, text, 1); + return 1; /* The proto object */ +} diff --git a/javascript/duktape/window.c b/javascript/duktape/window.c index efba5851c..e52fcb00d 100644 --- a/javascript/duktape/window.c +++ b/javascript/duktape/window.c @@ -57,22 +57,8 @@ static DUKKY_GETTER(window,document) { DUKKY_GET_METHOD_PRIVATE(window); LOG("priv=%p", priv); - duk_push_this(ctx); - duk_get_prop_string(ctx, -1, PROP_NAME(window, document)); - if (!duk_is_undefined(ctx, -1)) { - return 1; - } else { - duk_pop(ctx); - } dom_document *doc = priv->htmlc->document; - duk_push_pointer(ctx, doc); - if (dukky_create_object(ctx, PROTO_NAME(document), 1) != DUK_EXEC_SUCCESS) { - LOG("ERROR"); - } - duk_push_this(ctx); - duk_dup(ctx, -2); - duk_put_prop_string(ctx, -2, PROP_NAME(window, document)); - duk_pop(ctx); + dukky_push_node(ctx, (struct dom_node *)doc); return 1; } @@ -82,8 +68,13 @@ static DUKKY_SETTER(window,document) return 0; } +#define STEAL_THING(X) \ + duk_get_global_string(ctx, #X); \ + duk_put_prop_string(ctx, 0, #X) + DUKKY_FUNC(window, __proto) { + STEAL_THING(undefined); /* Populate window's prototypical functionality */ DUKKY_POPULATE_FULL_PROPERTY(window, document); /* Set this prototype's prototype (left-parent)*/ |