PageRenderTime 39ms CodeModel.GetById 21ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/src/erlv8_object.erl

http://github.com/beamjs/erlv8
Erlang | 120 lines | 90 code | 30 blank | 0 comment | 0 complexity | 1f549bd9432abfc96c0e6f3b30a99f99 MD5 | raw file
  1-module(erlv8_object).
  2
  3-include("erlv8.hrl").
  4
  5-export([vm/1,
  6
  7         proplist/1, set_value/3, set_value/4, set_hidden_value/3, get_value/2, get_value/3, get_hidden_value/2, get_hidden_value/3,
  8         internal_field_count/1, get_internal_field/2, set_internal_field/3,
  9         set_prototype/2, get_prototype/1, delete/2, set_accessor/3, set_accessor/4, set_accessor/5, set_accessor/6,
 10         equals/2, strict_equals/2, call/2, call/3,
 11
 12         new/1, new/2]).
 13
 14vm({Erlv8Obj,  _Resource, VM}) when ?is_v8(Erlv8Obj) ->
 15    VM.
 16
 17proplist({_Erlv8Obj,  Resource, VM}) ->
 18    erlv8_vm:enqueue_tick(VM,{proplist, Resource}).
 19
 20set_value(Key, Value, {_Erlv8Obj,  Resource, VM}) ->
 21    erlv8_vm:enqueue_tick(VM, {set, Resource, Key, Value}).
 22
 23set_value(Key,Value,PropertyAttribute, {_Erlv8Obj,  Resource, VM}) ->
 24    erlv8_vm:enqueue_tick(VM, {set, Resource, Key, Value, PropertyAttribute}).
 25
 26set_hidden_value(Key,Value, {_Erlv8Obj,  Resource, VM}) ->
 27    erlv8_vm:enqueue_tick(VM, {set_hidden, Resource, Key, Value}).
 28
 29get_value(Key, {_Erlv8Obj,  _Resource, _VM} = Self) ->
 30    get_value(Key, undefined, Self).
 31
 32get_value(Key, Default, {_Erlv8Obj,  Resource, VM}) ->
 33    case erlv8_vm:enqueue_tick(VM, {get, Resource, Key}) of
 34        undefined ->
 35            Default;
 36        Val ->
 37            Val
 38    end.
 39
 40get_hidden_value(Key, {_Erlv8Obj,  _Resource, _VM} = Self) ->
 41    get_hidden_value(Key, undefined, Self).
 42
 43get_hidden_value(Key, Default, {_Erlv8Obj,  Resource, VM}) ->
 44    case erlv8_vm:enqueue_tick(VM, {get_hidden, Resource, Key}) of
 45        undefined ->
 46            Default;
 47        Val ->
 48            Val
 49    end.
 50
 51internal_field_count({_Erlv8Obj,  Resource, VM}) ->
 52    erlv8_vm:enqueue_tick(VM, {internal_count, Resource}).
 53
 54get_internal_field(Index, {_Erlv8Obj,  Resource, VM}) ->
 55    erlv8_vm:enqueue_tick(VM, {get_internal, Resource, Index}).
 56
 57set_internal_field(Index, {extern, Value}, {_Erlv8Obj,  Resource, VM}) ->
 58    erlv8_vm:enqueue_tick(VM, {set_internal_extern, Resource, Index, Value, erlv8_extern:type(Value)});
 59
 60set_internal_field(Index, Value, {_Erlv8Obj,  Resource, VM}) ->
 61    erlv8_vm:enqueue_tick(VM, {set_internal, Resource, Index, Value}).
 62
 63set_prototype(Proto, {_Erlv8Obj,  Resource, VM}) ->
 64    erlv8_vm:enqueue_tick(VM, {set_proto, Resource, Proto}).
 65
 66get_prototype({_Erlv8Obj,  Resource, VM}) ->
 67    erlv8_vm:enqueue_tick(VM, {get_proto, Resource}).
 68
 69delete(Key, {_Erlv8Obj,  Resource, VM}) ->
 70    erlv8_vm:enqueue_tick(VM, {delete, Resource, Key}).
 71
 72set_accessor(Property, Getter, {_Erlv8Obj,  Resource, VM}) ->
 73    case erlv8_vm:enqueue_tick(VM, {set_accessor, Resource, Property, Getter}) of
 74        badarg ->
 75            throw(badarg);
 76        Result ->
 77            Result
 78    end.
 79
 80set_accessor(Property, Getter, Setter, {_Erlv8Obj,  Resource, VM}) ->
 81    case erlv8_vm:enqueue_tick(VM, {set_accessor, Resource, Property, Getter, Setter}) of
 82        badarg ->
 83            throw(badarg);
 84        Result ->
 85            Result
 86    end.
 87
 88set_accessor(Property, Getter, Setter, AccessControl, {_Erlv8Obj,  Resource, VM}) ->
 89    case erlv8_vm:enqueue_tick(VM, {set_accessor, Resource, Property, Getter, Setter, AccessControl}) of
 90        badarg ->
 91            throw(badarg);
 92        Result ->
 93            Result
 94    end.
 95
 96set_accessor(Property, Getter, Setter, AccessControl, PropertyAttribute, {_Erlv8Obj,  Resource, VM}) ->
 97    case erlv8_vm:enqueue_tick(VM, {set_accessor, Resource, Property, Getter, Setter, AccessControl, PropertyAttribute}) of
 98        badarg ->
 99            throw(badarg);
100        Result ->
101            Result
102    end.
103
104equals({_Tag,AnotherObject,_}, {_Erlv8Obj,  Resource, VM}) ->
105    erlv8_value:equals(VM, Resource, AnotherObject).
106
107strict_equals({_Tag,AnotherObject,_}, {_Erlv8Obj,  Resource, VM}) ->
108    erlv8_value:strict_equals(VM, Resource, AnotherObject).
109
110call(Fun, Self) ->
111    call(Fun,[], Self).
112
113call(Fun,Args, {_Erlv8Obj,  Resource, VM}) ->
114    Fun:call({erlv8_object, Resource,VM}, Args).
115
116new(O) ->
117    new(O,undefined).
118
119new(O, V) ->
120    #erlv8_object{resource = O, vm = V}.