Skip to content

Instantly share code, notes, and snippets.

@aziis98
Last active December 27, 2018 14:42
Show Gist options
  • Select an option

  • Save aziis98/ac819accb1bb1b64a2215a2f09223cd0 to your computer and use it in GitHub Desktop.

Select an option

Save aziis98/ac819accb1bb1b64a2215a2f09223cd0 to your computer and use it in GitHub Desktop.

WebVM (SmallTalk VM)

This is the actual definition of the custom VM to run web bytecode.

The various options for the VM are:

  • Stack based (like the JVM)
  • Register based (like LUA (?))
  • Functional (?) (Some Lisp-like thingy)

Nah, let's go for the stack based one that seems decent. So one bytecode can be one of the following operation

  • Draw operation
  • Aritmetic operation
  • Object operation

Conventions:

  • vm-stack: Actual stack with byte values
  • matrix-stack: render matrix stack

Draw Operations

Almost a wrapper for OpenGL primitives, all "work consistently" on HiDPI screens.

  • DRAW_LINES: Draws a 1px thick line, pops from the vm-stack the N number of points to draw and the following N pair of points (so 4 * N number popped in total).

Polygon) An integer telling the vertex count and a list of 2*N floats rappreseting all the coords.

  • DRAW_POLYGON: Fills the current polygon on the vm-stack
  • FILL_POLYGON: Fills the current polygon on the vm-stack

Arc) Used for ellipses, "border-radius" and etc. pops various params [TODO]

  • DRAW_ARC
  • FILL_ARC

Matrix) 6 values rappreseting the 2x3 matrix.

  • DRAW_PUSH_MATRIX: pushes a 2x3 matrix on the matrix transform stack
  • DRAW_POP_MATRIX: pops the most recent matrix off the stack (and pushes it on the vm stack?)

The last two are included for reducing the amount of code to tell the VM to run (and this will be the general frame of mind for new bytecodes)

  • PUSH_MASK_RECT: For now just a rect for easy intersection implementation
  • POP_MASK_RECT

Far future)

  • PUSH_SHADER: Pushes an OpenGL Shader (Maybe I mean "Program"?) (who knows the rappresentation for now...)
  • POP_SHADER: Pops the most recent shader on the shader-stack (Looks like a good idea to me have stacked shaders)

Object Operations

  • OBJECT_NEW: Takes a Symbol or something pointing to the class of the object to create, pushes the resulting pointer on the stack for usage. Also used to define Classes.

  • OBJECT_FREE: (Better being low level for now, so no GC) Takes an object reference/pointer.

  • OBJECT_GET: Takes an object and a property string and returns the corresponding value (better not to use this one because this would compute each time the byteoffset based on all the prototype chain)

  • OBJECT_SET

  • MEM_GET: Faster alternative, takes a pointer and the byte-size of the value to push on the stack.

  • MEM_SET: As before, for setting things fastly.

Aritmetic

  • PUSH_((U?INT)|FLOAT)(8|16|32|64): Pushes the following bytes on the stack [3 Operations]

  • PUSH_FEW_BYTES <bytes...>

  • PUSH_MANY_BYTES <count: UInt32> <bytes...> (Do directly load in memory pices of things, for example strings)

  • ((U?INT)|FLOAT)(8|16|32|64)_(ADD|SUB|MUL|DIV|MOD): All combinations of this for all the math stuff [60 Operations] (Maybe they are a bit more then expected - a possible optimization would be to place the op type in a separate byte in the program)

  • MATH <SQRT|SIN|COS|TAN|ATAN|ARCCOS|ARCSIN|LERP|...> <UINT16|FLOAT64|...> Pops the correct number of bytes from the vm-stack and pushes the result

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment