You are reading a single comment by @Gordon and its replies. Click here to read the full conversation.
  • I recently got asked some pretty deep questions about Espruino by e-mail - I'm copying here as they're probably useful to anyone hacking on Espruino...

    The structure of JsVar is: https://github.com/espruino/Espruino/blo­b/4e9f34831ff9f200cc5e261c30a2af9f145b98­0c/src/jsvar.h#L122

    The structure of JsVarData is : https://github.com/espruino/Espruino/blo­b/4e9f34831ff9f200cc5e261c30a2af9f145b98­0c/src/jsvar.h#L110

    For this structure of JsVarData, I do not understand the effect of “native“ and “ref”.

    'Native' is information for functions that are implemented as native code rather than JavaScript (so basically compiled C code). 'Ref' contains references to other children/siblings in the linked list/tree structure.

    And why does it use union structure. What is the advantage of the union structure?

    See http://en.wikipedia.org/wiki/Union_type#­C.2FC.2B.2B

    Basically unions overlap in memory, so all the elements of JsVarData share the same bytes. There is some documentation under the definition that describes how bytes are used for different types of variable:

    https://github.com/espruino/Espruino/blo­b/master/src/jsvar.h#L144

    Also:

    http://www.espruino.com/Internals

    What puzzled me most is the structure of JsVarDateRef: https://github.com/espruino/Espruino/blo­b/4e9f34831ff9f200cc5e261c30a2af9f145b98­0c/src/jsvar.h#L98

    What do the “nextSibling”, ”prevSibling”, ”refs”, ”firstChild” and “lastChild” mean? Could you give me an example for understanding every part of the JsVar structure wil be assigned?

    “nextSibling”, ”prevSibling” are when the JsVar is used in a linked list
    refs is for reference counting - it's the number of references to the JsVar - if that is 0 and there are no locks then the variable can be freed.
    ”firstChild” and “lastChild” point to the beginning and end of a linked list of children.

    The best way to get an idea of how they're all used is to type trace() in Espruino. This will dump out the datastructure in a human readable form (to see how, look at jsvTrace).

    And what is the whole structure between variable and another variable? A Binary tree or a list? Please give me an example.

    It's a linked list. But a JsVar can have children, in which case it is a linked list of children too.

    If I upload the code digitalWrite([A5,A6],0x11) to the board in the terminal, I do not know how the every part of JsVar Structure will be assigned for the array [A5,A6].

    [A5,A6] goes to JsVar(Array, JsVar(Name_int, 0, JsVar(Pin,A5)), JsVar(Name_int, 1, JsVar(Pin, A6)))

    (Note: the use of JsVar() is just pseudo-code to denote a tree structure)

    But arrays can be 'packed' - if you used a simple number like [4,5] you'd get: JsVar(Array, JsVar(Name_int_int, 0, 4), JsVar(Name_int_int, 1, 5))

    And how does the array [A5,A6] add to the JsExecIfo->root? It is a binary tree or a list, or some struct else?

    It actually doesn't go into JsExecIfo->root as it isn't a global variable, but it's stored as a linked list while executing the function digitalWrite

About

Avatar for Gordon @Gordon started