Skip to content

[Discussion] API to associate binding state (pointers) with the JerryScript context #1717

Closed
@martijnthe

Description

@martijnthe

A while ago, JERRY_CONTEXT define and jerry_context_t had been added.
Before that change, all the fields in jerry_context_t were statics that were sprinkled throughout the JerryScript code base. This wasn't great for projects where multiple instances of JerryScript are needed, because statics are by definition "singleton".

Luckily, the addition of the JERRY_CONTEXT define made it easier to create multiple instances of JerryScript. Typically, to support multiple instances, one would provide a custom definition of JERRY_CONTEXT, for example to look up the current jerry_context_t in thread local storage.

However, often, binding code also needs to keep state regarding the native resources that it uses. In a multi-context scenario, each instance of JerryScript needs to have its own "instance" of this binding state as well. Therefore, using static variables cannot be used. Again, because static equals "singleton".

Using __thread or a platform-specific thread local storage (TLS) API could be used of course, but this would make the binding code less portable / re-usable.

Therefore, I think it make sense to try to come up with an API to associate binding state with a JerryScript context in an easy way that promotes re-use and portability of the binding code.

Some design goals that I have in mind for this an API to associate binding state with a JerryScript context:

  • enable multiple bindings to each associate their own state with a JerryScript context.
  • making binding code "portable" / re-usable: avoid __thread or a platform specific TLS API.
  • should not require having to change JerryScript code (incl. jcontext.[h|c]).
  • "scales down": no bloat when using a single-context (same speed/RAM/code size as today).
  • easy to use from a developer point of view (probably not as easy as static, but hopefully close?)

I imagining something like a void *user_binding_state[] array in JERRY_CONTEXT, plus a couple of jerry_... functions to register/allocate/get/free the state for a particular binding. But I'm not quite sure how such a solution would "scale down".

Thoughts?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions