Skip to content
This repository

Data types 

yrashk edited this page · 12 revisions
Clone this wiki locally

Data Types

Primitive types

Primitive types are normally converted into native corresponding types.

String

Global:set_value("str","Hello"),
Global:get_value("str"). % results in
"Hello"

Please note that all non-special atoms (i.e. not something along the line of true, false, undefined, null) are automatically converted to strings when they go into the JavaScript VM:

Global:set_value("atom",hello),
Global:get_value("atom"). % results in
"hello"

Number

Global:set_value("num",123),
Global:get_value("num"). % results in
123

Boolean

Global:set_value("bool",true),
Global:get_value("bool"). % results in
true

undefined & null

Global:set_value("undefined",undefined),
Global:get_value("undefined"). % results in
undefined
Global:set_value("null",null),
Global:get_value("null"). % results in
undefined

List

Erlang lists are always treated as strings (unless they absolutely can not be decoded into latin1). For arrays, take a look at erlv8_array and ?V8Arr.

Tuple

Since there are no tuples in JavaScript, passing a tuple into JavaScript will return you undefined:

Global:set_value("tuple",{1,2,3}),
Global:get_value("tuple"). % results in
undefined

Object

Object is a little bit more complicated beast. You can create object out of proplist:

erlv8_object:new([{"prop","val"}]), %% or, an equivalent macro:
?V8Obj([{"prop","val"}])

but once it gets to JavaScript it becomes a special kind of value that you will need a special (yet simple) API to manipulate it. Here's the example:

Global:set_value("obj",erlv8_object:new([{"prop","val"}])).
Global:get_value("obj"). % returns:
{erlv8_object,<<>>}

Now you should use an API provided by erlv8_object:

Obj = Global:get_value("obj"),
Obj:set_value("x",1),
Obj:get_value("x"). % returns
1
Obj:proplist(). % returns
[{"x",1}]

You can also set and get object's prototype by using erlv8_object's set_prototype/1 and get_prototype/0.

Object.method()

You can call a method directly from an object:

Obj:call(Obj:get_value("method"),[])

Array

Arrays are in fact objects with few extra functions available

Global:set_value("arr",?V8Arr([1,2,3])),
Arr = erlv8_vm:taint(?V8Arr([1,2,3])),
Arr:push(4),
Arr:length(). % returns
4

Function

Function is an interesting case, too. Again, you can define a function of a certain specification by simply passing a fun:

Global:set_value("fun",fun(_,Args) -> Args end).

The specification is

-spec (Invocation::#erlv8_fun_invocation,Arguments::list(term())) -> term().

If you'll try to call it from javascript, it will do what you expect it to do — it will return a list of arguments:

erlv8_vm:run(VM,"fun(1,2,3);").  % returns
{ok,[1,2,3]}

You can also call JavaScript function from Erlang:

erlv8_vm:run(VM,"f = function () { return 100; }").
F = Global:get_value("f").
F:call(). % results in
100

Please note that if you want to pass arguments into F:call() you should pass them as a list:

F:call([1,2,3])

Also, if you want to specify a receiver (i.e. this), just specify your receiver object before the arguments:

F:call(This,[1,2,3])

Function Object

erlv8_fun extends erlv8_objects so all functions available for objects are available for the function as well (just like JavaScript):

F:set_value("x",123),
erlv8_vm:run(VM,"f(f.x)"). % results in
{ok,[123]}

You can also easily get access to function's object explicitly:

FObj = F:object(),
FObj:set_value("x",123),
erlv8_vm:run(VM,"f(f.x)"). % results in
{ok,[123]}

Pid

Pids are not native to JavaScript, yet they can be sent to the VM like any other value.

Reference

References are not native to JavaScript, yet they can be sent to the VM like any other value (just like pids).

Something went wrong with that request. Please try again.