Class: Jsrb::Base
- Inherits:
-
Object
- Object
- Jsrb::Base
- Defined in:
- lib/jsrb/base.rb
Overview
Jsrb::Base is a centralized class for Jsrb template.
js, accessed from views (i.e. *.jsrb files), is an instance of Jsrb::Base.
Jsrb::Base provides the interface for pushing statements,
constructing expressions and generating JavaScript outputs
with handling an internal statement context properly.
Class Attribute Summary collapse
-
.code_generator ⇒ String
Shows JavaScript generator class name,
'Jsrb::NotFastGenerator'by default.
Instance Method Summary collapse
-
#do!(expr) ⇒ nil
Pushes an ExpressionStatement to the current context.
-
#expr(object = nil) ⇒ Jsrb::ExprChain
Constructs a new expression chain with a given JavaScript AST node.
-
#generate_code ⇒ String
Generates executable JavaScript code from current context.
-
#if(cond_expr) { ... } ⇒ Jsrb::CondChain
Constructs a new conditional chain that represents a conditional expression at end.
-
#if!(cond_expr) { ... } ⇒ Jsrb::CondChain
Starts a new conditional chain that pushes an IfStatement to the current context at end.
-
#set!(lhs, rhs) ⇒ nil
Pushes an assignment statement
lhs = rhs;. -
#var!(id = nil) { ... } ⇒ Jsrb::ExprChain
Pushes a VariableDeclaration to the current context and returns an access to the created identifier.
Class Attribute Details
.code_generator ⇒ String
Shows JavaScript generator class name, 'Jsrb::NotFastGenerator' by default.
Help wanted!
Jsrb::NotFastGenerator uses ExecJS and escodegen to generate JavaScript. It could be more efficient and get better error messages if we implement it in Ruby.
218 219 220 |
# File 'lib/jsrb/base.rb', line 218 def code_generator @code_generator || 'Jsrb::NotFastGenerator' end |
Instance Method Details
#do!(expr) ⇒ nil
Pushes an ExpressionStatement to the current context
36 37 38 39 40 41 42 43 44 |
# File 'lib/jsrb/base.rb', line 36 def do!(expr) ast = expr.object raise ArgumentError, 'Expression is empty' unless ast @context.push( type: 'ExpressionStatement', expression: ast ) end |
#expr(object = nil) ⇒ Jsrb::ExprChain
Constructs a new expression chain with a given JavaScript AST node.
203 204 205 |
# File 'lib/jsrb/base.rb', line 203 def expr(object = nil) @context.new_expression(object) end |
#generate_code ⇒ String
Generates executable JavaScript code from current context.
20 21 22 23 24 25 |
# File 'lib/jsrb/base.rb', line 20 def generate_code generator = self.class.code_generator_class.new generator.call type: 'Program', sourceType: 'script', body: @context.stacks.first end |
#if(cond_expr) { ... } ⇒ Jsrb::CondChain
Constructs a new conditional chain that represents a conditional expression at end.
194 195 196 |
# File 'lib/jsrb/base.rb', line 194 def if(cond_expr, &block) CondChain.new(@context, true).elsif(cond_expr, &block) end |
#if!(cond_expr) { ... } ⇒ Jsrb::CondChain
Starts a new conditional chain that pushes an IfStatement to the current context at end.
161 162 163 |
# File 'lib/jsrb/base.rb', line 161 def if!(cond_expr, &block) CondChain.new(@context, false).elsif(cond_expr, &block) end |
#set!(lhs, rhs) ⇒ nil
Pushes an assignment statement lhs = rhs;.
This is a short hand of js.do! lhs_expr.set(rhs_expr)
63 64 65 66 |
# File 'lib/jsrb/base.rb', line 63 def set!(lhs, rhs) lhs_expr = lhs.is_a?(ExprChain) ? lhs : expr(lhs) do! lhs_expr.set(rhs) end |
#var!(id = nil) { ... } ⇒ Jsrb::ExprChain
Pushes a VariableDeclaration to the current context and returns an access to the created identifier.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/jsrb/base.rb', line 87 def var!(id = nil) id ||= @context.gen_var_name! val_ast = if block_given? raw_expr = yield raw_expr.is_a?(ExprChain) ? raw_expr.unwrap : @context.ruby_to_js_ast(raw_expr) end if val_ast @context.push( type: 'VariableDeclaration', declarations: [{ type: 'VariableDeclarator', id: { type: 'Identifier', name: id.to_s }, init: val_ast }], kind: 'var' ) else @context.push( type: 'VariableDeclaration', declarations: [{ type: 'VariableDeclarator', id: { type: 'Identifier', name: id.to_s } }], kind: 'var' ) end expr[id] end |