Class: Phuby::Runtime

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/phuby/runtime.rb,
ext/phuby/phuby_runtime.c

Defined Under Namespace

Classes: NotStartedError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRuntime

Returns a new instance of Runtime.



14
15
16
17
18
# File 'lib/phuby/runtime.rb', line 14

def initialize
  @events       = Events.new
  @mutex        = Mutex.new
  @proxy_map    = {}
end

Instance Attribute Details

#eventsObject

Returns the value of attribute events.



8
9
10
# File 'lib/phuby/runtime.rb', line 8

def events
  @events
end

Class Method Details

.php(&block) ⇒ Object



10
11
12
# File 'lib/phuby/runtime.rb', line 10

def self.php &block
  instance.php(&block)
end

Instance Method Details

#[](key) ⇒ Object

Raises:



48
49
50
51
# File 'lib/phuby/runtime.rb', line 48

def [] key
  raise NotStartedError, "please start the runtime" unless @mutex.locked?
  get key
end

#[]=(key, value) ⇒ Object

Raises:



53
54
55
56
# File 'lib/phuby/runtime.rb', line 53

def []= key, value
  raise NotStartedError, "please start the runtime" unless @mutex.locked?
  set key, value
end

#eval(string_or_io, filename = nil) ⇒ Object

Raises:



31
32
33
34
35
36
37
38
39
# File 'lib/phuby/runtime.rb', line 31

def eval string_or_io, filename = nil
  raise NotStartedError, "please start the runtime" unless @mutex.locked?

  if string_or_io.respond_to? :read
    native_eval_io string_or_io, filename || string_or_io.path
  else
    native_eval string_or_io, filename || __FILE__
  end
end

#php(&block) ⇒ Object



41
42
43
44
45
46
# File 'lib/phuby/runtime.rb', line 41

def php &block
  start
  block.call(self)
ensure
  stop
end

#startObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'ext/phuby/phuby_runtime.c', line 108

static VALUE start(VALUE self)
{
  VALUE mutex = rb_iv_get(self, "@mutex");
  rb_funcall(mutex, rb_intern("lock"), 0);

  /* FIXME:
   * I got these from the book.  I don't know wtf they're for yet. */
  int argc = 1;
  char *argv[2] = { "embed4", NULL };
  /* end FIXME */

  php_embed_module.ub_write       = phuby_ub_write;
  php_embed_module.header_handler = phuby_header_handler;
  php_embed_module.send_headers   = phuby_send_headers;
  php_embed_module.flush          = phuby_flush;

  php_embed_init(argc, argv);
  zend_class_entry ce;
  INIT_CLASS_ENTRY(ce, "RubyProxy", php_ruby_functions);

  php_ruby_proxy = zend_register_internal_class(&ce);

  SG(headers_sent) = 0;
  SG(request_info).no_headers = 0;

  return Qnil;
}

#started?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/phuby/runtime.rb', line 20

def started?
  @mutex.locked?
end

#stopObject



136
137
138
139
140
141
142
143
144
# File 'ext/phuby/phuby_runtime.c', line 136

static VALUE stop(VALUE self)
{
  php_embed_shutdown();

  VALUE mutex = rb_iv_get(self, "@mutex");
  rb_funcall(mutex, rb_intern("unlock"), 0);

  return Qnil;
}

#with_events(event) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



24
25
26
27
28
29
# File 'lib/phuby/runtime.rb', line 24

def with_events event
  old = @events
  @events = event
  yield self
  @events = old
end