diff --git a/README.md b/README.md index 17eea20..ae2df8e 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,6 @@ # MiniRacer -Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/mini_racer`. To experiment with that code, run `bin/console` for an interactive prompt. - -TODO: Delete this and the text above, and describe your gem +Minimal embedded V8 for Ruby ## Installation @@ -22,13 +20,9 @@ Or install it yourself as: ## Usage -TODO: Write usage instructions here ## Development -After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. - -To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org). ## Contributing diff --git a/ext/mini_racer_extension/extconf.rb b/ext/mini_racer_extension/extconf.rb index be1fe19..6ee7679 100644 --- a/ext/mini_racer_extension/extconf.rb +++ b/ext/mini_racer_extension/extconf.rb @@ -20,22 +20,33 @@ end LIBV8_COMPATIBILITY = '~> 5.0.71.35.0' -begin - require 'rubygems' - gem 'libv8', LIBV8_COMPATIBILITY -rescue Gem::LoadError - warn "Warning! Unable to load libv8 #{LIBV8_COMPATIBILITY}." -rescue LoadError - warn "Warning! Could not load rubygems. Please make sure you have libv8 #{LIBV8_COMPATIBILITY} installed." -ensure - require 'libv8' -end +# begin +# require 'rubygems' +# gem 'libv8', LIBV8_COMPATIBILITY +# rescue Gem::LoadError +# warn "Warning! Unable to load libv8 #{LIBV8_COMPATIBILITY}." +# rescue LoadError +# warn "Warning! Could not load rubygems. Please make sure you have libv8 #{LIBV8_COMPATIBILITY} installed." +# ensure +# require 'libv8' +# end +# +# Libv8.configure_makefile -Libv8.configure_makefile +NODE_PATH = "/home/sam/Source/libv8" +NODE_INCLUDE = NODE_PATH + "/vendor/v8/include" +NODE_LIBS = NODE_PATH + "/vendor/v8/out/x64.release/obj.target/tools/gyp" + +$INCFLAGS.insert 0, "-I#{NODE_INCLUDE} -I#{NODE_PATH}/vendor/v8 " +$LDFLAGS.insert 0, " #{NODE_LIBS}/libv8_base.a #{NODE_LIBS}/libv8_libbase.a #{NODE_LIBS}/libv8_snapshot.a #{NODE_LIBS}/libv8_libplatform.a " + +dir_config('v8') +find_header('v8.h') +have_library('v8') # Temp Hack -find_header('v8.h', '/home/sam/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/libv8-5.0.71.35.0/vendor/v8/include') +#find_header('v8.h', '/home/sam/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/libv8-5.0.71.35.0/vendor/v8/include') -find_header('libplatform/libplatform.h', '/home/sam/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/libv8-5.0.71.35.0/vendor/v8/include') +#find_header('libplatform/libplatform.h', '/home/sam/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/libv8-5.0.71.35.0/vendor/v8/include') create_makefile 'mini_racer_extension' diff --git a/ext/mini_racer_extension/mini_racer_extension.cc b/ext/mini_racer_extension/mini_racer_extension.cc index 233faff..7923846 100644 --- a/ext/mini_racer_extension/mini_racer_extension.cc +++ b/ext/mini_racer_extension/mini_racer_extension.cc @@ -1,11 +1,29 @@ #include #include +#include #include #include #include using namespace v8; +typedef struct { + Isolate* isolate; + Persistent* context; +} ContextInfo; + +typedef struct { + bool parsed; + bool executed; + Persistent* value; +} EvalResult; + +typedef struct { + ContextInfo* context_info; + Local* eval; + EvalResult* result; +} EvalParams; + class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator { public: virtual void* Allocate(size_t length) { @@ -19,7 +37,6 @@ class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator { Platform* current_platform = NULL; ArrayBufferAllocator* allocator = NULL; -Isolate* isolate = NULL; static void init_v8() { if (current_platform == NULL) { @@ -27,12 +44,6 @@ static void init_v8() { current_platform = platform::CreateDefaultPlatform(); V8::InitializePlatform(current_platform); V8::Initialize(); - allocator = new ArrayBufferAllocator(); - - Isolate::CreateParams create_params; - create_params.array_buffer_allocator = allocator; - isolate = Isolate::New(create_params); - } } @@ -41,70 +52,145 @@ void shutdown_v8() { } -static VALUE rb_context_eval(VALUE self, VALUE str) { - init_v8(); +void* +nogvl_context_eval(void* arg) { + EvalParams* eval_params = (EvalParams*)arg; + EvalResult* result = eval_params->result; - Isolate::Scope isolate_scope(isolate); - HandleScope handle_scope(isolate); - Persistent* persistent_context; - - Data_Get_Struct(self, Persistent, persistent_context); - Local context = Local::New(isolate, *persistent_context); + Isolate::Scope isolate_scope(eval_params->context_info->isolate); + HandleScope handle_scope(eval_params->context_info->isolate); + Local context = Local::New(eval_params->context_info->isolate, + *eval_params->context_info->context); Context::Scope context_scope(context); - Local eval = String::NewFromUtf8(isolate, RSTRING_PTR(str), + MaybeLocal