NetSurf
Typedefs | Functions
js.h File Reference

Interface to javascript engine functions. More...

#include "utils/errors.h"
Include dependency graph for js.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef struct jsheap jsheap
 JavaScript interpreter heap. More...
 
typedef struct jsthread jsthread
 JavaScript interpreter thread. More...
 

Functions

void js_initialise (void)
 Initialise javascript interpreter. More...
 
void js_finalise (void)
 finalise javascript interpreter More...
 
nserror js_newheap (int timeout, jsheap **heap)
 Create a new javascript heap. More...
 
void js_destroyheap (jsheap *heap)
 Destroy a previously created heap. More...
 
nserror js_newthread (jsheap *heap, void *win_priv, void *doc_priv, jsthread **thread)
 Create a new javascript thread. More...
 
nserror js_closethread (jsthread *thread)
 Close a javascript thread. More...
 
void js_destroythread (jsthread *thread)
 Destroy a javascript thread. More...
 
bool js_exec (jsthread *thread, const uint8_t *txt, size_t txtlen, const char *name)
 execute some javascript in a context More...
 
bool js_fire_event (jsthread *thread, const char *type, struct dom_document *doc, struct dom_node *target)
 fire an event at a dom node More...
 
bool js_dom_event_add_listener (jsthread *thread, struct dom_document *document, struct dom_node *node, struct dom_string *event_type_dom, void *js_funcval)
 
void js_handle_new_element (jsthread *thread, struct dom_element *node)
 Handle a new element being created. More...
 
void js_event_cleanup (jsthread *thread, struct dom_event *evt)
 Handle an event propagation finished callback. More...
 

Detailed Description

Interface to javascript engine functions.

Definition in file js.h.

Typedef Documentation

◆ jsheap

typedef struct jsheap jsheap

JavaScript interpreter heap.

In order to try and be moderately performant, we create a heap per browser window. This heap is shared by all browsing contexts we end up creating in that window.

Definition at line 41 of file js.h.

◆ jsthread

typedef struct jsthread jsthread

JavaScript interpreter thread.

When we create a browsing context itself (window+content) we have to create a JS thread to attach to the browsing context.

JS threads are associated with heaps and will be destroyed when the heap is destroyed. They can be shut down manually though and should be for object lifetime safety reasons.

Definition at line 53 of file js.h.

Function Documentation

◆ js_closethread()

nserror js_closethread ( jsthread thread)

Close a javascript thread.

This should be called when the HTML content which owns the thread is being closed. This is a separate process from destroying the thread and merely disconnects any callbacks and thus hopefully stops additional JS things from triggering. If any code runs and attempts to register callbacks after closedown, they will fail.

Parameters
threadThe thread to close down
Returns
NSERROR_OK on success, appropriate error otherwise

Definition at line 761 of file dukky.c.

References CTX, duk_get_global_string(), duk_get_top(), duk_set_top(), dukky_pcall(), jsthread::heap, MAGIC, NSERROR_OK, and NSLOG.

Referenced by html_close(), and html_stop().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ js_destroyheap()

void js_destroyheap ( jsheap heap)

Destroy a previously created heap.

Parameters
heapThe heap to destroy

Definition at line 638 of file dukky.c.

References dukky_destroyheap(), jsheap::live_threads, and jsheap::pending_destroy.

Referenced by browser_window_destroy_internal().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ js_destroythread()

void js_destroythread ( jsthread thread)

Destroy a javascript thread.

This should be called when the browsing context is done with the thread.

This will be called when the HTML content associated with the browsing context is being destroyed. The thread should have already been closed down during the HTML content close.

Parameters
threadThe thread to be destroyed

Definition at line 815 of file dukky.c.

References dukky_destroythread(), jsthread::in_use, and jsthread::pending_destroy.

Referenced by html_destroy(), and js_newthread().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ js_dom_event_add_listener()

bool js_dom_event_add_listener ( jsthread thread,
struct dom_document *  document,
struct dom_node *  node,
struct dom_string *  event_type_dom,
void *  js_funcval 
)

◆ js_event_cleanup()

void js_event_cleanup ( jsthread thread,
struct dom_event *  evt 
)

Handle an event propagation finished callback.

This is called once an event finishes propagating, no matter how it finishes. The intent here is that the JS context can perform any cleanups it may need to perform before the DOM finishes and the event may end up freed.

Definition at line 1551 of file dukky.c.

References CTX, duk_del_prop(), duk_get_global_string(), duk_pop(), duk_push_pointer(), dukky_enter_thread(), dukky_leave_thread(), and EVENT_MAGIC.

Referenced by dom_default_action_finished_cb(), and js_fire_event().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ js_exec()

bool js_exec ( jsthread thread,
const uint8_t *  txt,
size_t  txtlen,
const char *  name 
)

execute some javascript in a context

Definition at line 922 of file dukky.c.

References CTX, DUK_COMPILE_EVAL, DUK_EXEC_ERROR, duk_get_boolean(), duk_get_top(), duk_pcall(), duk_pcompile_lstring_filename, duk_push_boolean(), duk_push_string(), duk_set_top(), dukky_dump_error(), dukky_enter_thread(), dukky_leave_thread(), dukky_reset_start_time(), NSLOG, jsthread::pending_destroy, and PRIsizet.

Referenced by select_script_handler().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ js_finalise()

void js_finalise ( void  )

finalise javascript interpreter

Definition at line 590 of file dukky.c.

Referenced by netsurf_exit().

Here is the caller graph for this function:

◆ js_fire_event()

bool js_fire_event ( jsthread thread,
const char *  type,
struct dom_document *  doc,
struct dom_node *  target 
)

fire an event at a dom node

Todo:
Make this more generic, this only handles load and only targetting the window, so that we actually stand a chance of getting 3.4 out.

Definition at line 1567 of file dukky.c.

References CTX, duk_get_global_string(), duk_get_prop(), duk_get_prop_string(), duk_insert(), duk_is_undefined(), duk_pcall_method(), duk_pop(), duk_pop_n(), duk_push_global_object(), duk_push_lstring(), duk_safe_to_string, dukky_enter_thread(), dukky_get_current_value_of_event_handler(), dukky_leave_thread(), dukky_push_event(), dukky_push_node(), dukky_reset_start_time(), HANDLER_MAGIC, js_event_cleanup(), NSLOG, and type.

Referenced by html_finish_conversion().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ js_handle_new_element()

void js_handle_new_element ( jsthread thread,
struct dom_element *  node 
)

Handle a new element being created.

This is called once an element is inserted into the DOM document handled by the context provided. The JS implementation must then scan the element for on* attributes and register appropriate listeners for those handlers.

Definition at line 1471 of file dukky.c.

References CTX, dukky_enter_thread(), dukky_leave_thread(), and dukky_register_event_listener_for().

Referenced by dom_default_action_DOMNodeInserted_cb().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ js_initialise()

void js_initialise ( void  )

Initialise javascript interpreter.

TODO: Forces JS on for our testing, needs changing before a release lest we incur the wrath of others.

Definition at line 577 of file dukky.c.

References javascript_init().

Referenced by netsurf_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ js_newheap()

nserror js_newheap ( int  timeout,
jsheap **  heap 
)

Create a new javascript heap.

There is usually one heap per browser window.

Parameters
timeoutelapsed wallclock time (in seconds) before callback is called
heapUpdated to the created JS heap
Returns
NSERROR_OK on success, appropriate error otherwise.

Definition at line 598 of file dukky.c.

References jsheap::ctx, duk_create_heap(), duk_push_boolean(), duk_push_global_object(), duk_push_object(), duk_put_global_string(), duk_put_prop_string(), dukky_alloc_function(), dukky_free_function(), dukky_realloc_function(), NSERROR_NOMEM, NSERROR_OK, NSLOG, and THREAD_MAP.

Referenced by browser_window_initialise_common().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ js_newthread()

nserror js_newthread ( jsheap heap,
void *  win_priv,
void *  doc_priv,
jsthread **  thread 
)

Create a new javascript thread.

This is called once for a page with javascript script tags on it. It constructs a fresh global window object and prepares the JS browsing context. It's important that threads are shut down cleanly when the browsing context is going to be cleaned up.

Parameters
heapThe heap to create the thread within
win_privThe value to give to the Window constructor as the window
doc_privThe value to give to the Document constructor as the document
threadUpdated to the created thread
Returns
NSERROR_OK on success, appropriate error otherwise

Definition at line 650 of file dukky.c.

References jsheap::ctx, jsthread::ctx, CTX, DUK_COMPILE_EVAL, duk_del_prop_string(), duk_get_global_string(), duk_get_prop_string(), duk_pcompile_lstring_filename, duk_pop(), duk_push_global_object(), duk_push_int(), duk_push_null(), duk_push_object(), duk_push_pointer(), duk_push_string(), duk_push_thread, duk_put_global_string(), duk_put_prop(), duk_put_prop_index(), duk_put_prop_string(), duk_require_context(), duk_safe_to_string, duk_set_global_object(), dukky_create_object(), dukky_log_stack_frame(), dukky_pcall(), EVENT_MAGIC, GENERICS_MAGIC, jsthread::heap, js_destroythread(), jsheap::live_threads, jsheap::next_thread, NSERROR_INIT_FAILED, NSERROR_NOMEM, NSERROR_OK, NSLOG, jsheap::pending_destroy, jsthread::thread_idx, and THREAD_MAP.

Referenced by browser_window_callback().

Here is the call graph for this function:
Here is the caller graph for this function: