Class: Scarpe::WASMInterops

Inherits:
Object
  • Object
show all
Includes:
Log
Defined in:
lib/scarpe/wasm/wasm_calls.rb

Instance Method Summary collapse

Constructor Details

#initializeWASMInterops

Returns a new instance of WASMInterops.



9
10
11
# File 'lib/scarpe/wasm/wasm_calls.rb', line 9

def initialize
  log_init("WASM")
end

Instance Method Details

#bind(name, func = nil, &block) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/scarpe/wasm/wasm_calls.rb', line 39

def bind(name, func = nil, &block)
  JS.global[:self][name.to_sym] = proc do |*args|
    args = args.map { |x| rubify(x) }
    (func ? func : block).call(*args)
  rescue => e
    @log.error(e)
    @log.error(e.backtrace)
  end
  defined?(JS.global[:self][name.to_sym])
end

#eval(command) ⇒ Object



50
51
52
# File 'lib/scarpe/wasm/wasm_calls.rb', line 50

def eval(command)
  JS.eval(command)
end

#init(command) ⇒ Object



54
55
56
57
# File 'lib/scarpe/wasm/wasm_calls.rb', line 54

def init(command)
  @commands ||= []
  @commands << command
end

#js_integer?(num) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
17
18
19
# File 'lib/scarpe/wasm/wasm_calls.rb', line 13

def js_integer?(num)
  if JS.global[:Number].isInteger(num)
    true
  else
    false
  end
end


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/scarpe/wasm/wasm_calls.rb', line 68

def navigate(string)
  # JS.eval('var empty =
  # `body {
  #   font-family: arial, Helvetica, sans-serif;
  #   margin: 0;
  #   height: 100%;
  #   overflow: hidden;
  # }
  # p {
  #   margin: 0;
  # }`;')
  # JS.eval("var head = document.head || document.getElementsByTagName('head')[0];")
  # JS.eval("var style = document.createElement('style');")
  # JS.eval("head.appendChild(style);")
  # JS.eval("style.type = 'text/css';")
  # # JS.eval("style.id = 'style-wvroot';")
  # JS.eval("head.id = 'head-wvroot';")
  # JS.eval("head.appendChild(style);")
  # JS.eval("style.appendChild(document.createTextNode(empty));")
  # JS.eval("var body = document.body;")
  # JS.eval("body.id = 'body-wvroot';")
  # JS.eval("var newDiv = document.createElement('div');")
  # JS.eval("newDiv.id = 'wrapper-wvroot';")
  # JS.eval("body.appendChild(newDiv);")

  style = JS.global[:document].createElement("style")
  style[:id] = "style-wvroot"
  style.appendChild(JS.global[:document].createTextNode("body {
    font-family: arial, Helvetica, sans-serif;
    margin: 0;
    height: 100%;
  }
  p {
    margin: 0;
  }"))
  JS.global[:document][:head].appendChild(style)
  JS.global[:document][:head][:id] = "head-wvroot"
  div = JS.global[:document].createElement("div")
  div[:id] = "wrapper-wvroot"
  JS.global[:document][:body].appendChild(div)
end

#rubify(obj) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/scarpe/wasm/wasm_calls.rb', line 21

def rubify(obj)
  type = obj.typeof
  case type
  when "string"
    obj.to_s
  when "number"
    if js_integer?(obj)
      obj.to_i
    else
      obj.to_f
    end
  when "undefined"
    nil
  else
    obj
  end
end

#runObject



110
111
112
# File 'lib/scarpe/wasm/wasm_calls.rb', line 110

def run
  @commands.each { |command| JS.eval(command) }
end

#set_size(width, height, hint) ⇒ Object



63
64
65
66
# File 'lib/scarpe/wasm/wasm_calls.rb', line 63

def set_size(width, height, hint)
  #JS.global[:document][:body][:style][:width] = width
  #JS.global[:document][:body][:style][:height] = height
end

#set_title(title) ⇒ Object



59
60
61
# File 'lib/scarpe/wasm/wasm_calls.rb', line 59

def set_title(title)
  JS.global[:document][:title] = title
end

#terminateObject



114
115
116
# File 'lib/scarpe/wasm/wasm_calls.rb', line 114

def terminate
  # stub
end