From fcbf63e62c627deae76c1b8cb8c0876c536ed811 Mon Sep 17 00:00:00 2001
From: Jari Vetoniemi
Date: Mon, 16 Mar 2020 18:49:26 +0900
Subject: Fresh start
---
jni/ruby/nacl/GNUmakefile.in | 100 ++++++
jni/ruby/nacl/README.nacl | 51 +++
jni/ruby/nacl/create_nmf.rb | 70 +++++
jni/ruby/nacl/dirent.h | 15 +
jni/ruby/nacl/example.html | 150 +++++++++
jni/ruby/nacl/nacl-config.rb | 61 ++++
jni/ruby/nacl/package.rb | 113 +++++++
jni/ruby/nacl/pepper_main.c | 732 +++++++++++++++++++++++++++++++++++++++++++
jni/ruby/nacl/resource.h | 8 +
jni/ruby/nacl/select.h | 7 +
jni/ruby/nacl/signal.h | 6 +
jni/ruby/nacl/stat.h | 10 +
jni/ruby/nacl/unistd.h | 9 +
jni/ruby/nacl/utime.h | 11 +
14 files changed, 1343 insertions(+)
create mode 100644 jni/ruby/nacl/GNUmakefile.in
create mode 100644 jni/ruby/nacl/README.nacl
create mode 100644 jni/ruby/nacl/create_nmf.rb
create mode 100644 jni/ruby/nacl/dirent.h
create mode 100644 jni/ruby/nacl/example.html
create mode 100755 jni/ruby/nacl/nacl-config.rb
create mode 100644 jni/ruby/nacl/package.rb
create mode 100644 jni/ruby/nacl/pepper_main.c
create mode 100644 jni/ruby/nacl/resource.h
create mode 100644 jni/ruby/nacl/select.h
create mode 100644 jni/ruby/nacl/signal.h
create mode 100644 jni/ruby/nacl/stat.h
create mode 100644 jni/ruby/nacl/unistd.h
create mode 100644 jni/ruby/nacl/utime.h
(limited to 'jni/ruby/nacl')
diff --git a/jni/ruby/nacl/GNUmakefile.in b/jni/ruby/nacl/GNUmakefile.in
new file mode 100644
index 0000000..1fffcee
--- /dev/null
+++ b/jni/ruby/nacl/GNUmakefile.in
@@ -0,0 +1,100 @@
+# Copyright 2012 Google Inc. All Rights Reserved.
+# Author: yugui@google.com (Yugui Sonoda)
+
+include Makefile
+-include uncommon.mk
+
+NACL_SDK_ROOT=@NACL_SDK_ROOT@
+NACL_TOOLCHAIN=@NACL_TOOLCHAIN@
+NACL_TOOLCHAIN_DIR=$(NACL_SDK_ROOT)/toolchain/$(NACL_TOOLCHAIN)
+CXX=@CXX@
+
+# Don't override CC/LD/etc if they are already set to absolute
+# paths (this is the case when building in the naclports tree).
+ifeq ($(dir $(CC)),./)
+CC:=$(NACL_TOOLCHAIN_DIR)/bin/$(CC)
+endif
+ifeq ($(dir $(CXX)),./)
+CXX:=$(NACL_TOOLCHAIN_DIR)/bin/$(CXX)
+endif
+ifeq ($(dir $(LD)),./)
+LD:=$(NACL_TOOLCHAIN_DIR)/bin/$(LD)
+endif
+ifeq ($(dir $(NM)),./)
+NM:=$(NACL_TOOLCHAIN_DIR)/bin/$(NM)
+endif
+ifeq ($(dir $(AR)),./)
+AR:=$(NACL_TOOLCHAIN_DIR)/bin/$(AR)
+endif
+ifeq ($(dir $(AS)),./)
+AS:=$(NACL_TOOLCHAIN_DIR)/bin/$(AS)
+endif
+ifeq ($(dir $(RANLIB)),./)
+RANLIB:=$(NACL_TOOLCHAIN_DIR)/bin/$(RANLIB)
+endif
+ifeq ($(dir $(OBJDUMP)),./)
+OBJDUMP:=$(NACL_TOOLCHAIN_DIR)/bin/$(OBJDUMP)
+endif
+ifeq ($(dir $(OBJCOPY)),./)
+OBJCOPY:=$(NACL_TOOLCHAIN_DIR)/bin/$(OBJCOPY)
+endif
+PYTHON=@PYTHON@
+
+PPROGRAM=pepper-$(PROGRAM)
+PEPPER_LIBS=-lppapi -lnacl_io
+PROGRAM_NMF=$(PROGRAM:$(EXEEXT)=.nmf)
+PPROGRAM_NMF=$(PPROGRAM:$(EXEEXT)=.nmf)
+
+GNUmakefile: $(srcdir)/nacl/GNUmakefile.in
+$(PPROGRAM): $(PROGRAM) pepper_main.$(OBJEXT)
+ $(Q)$(MAKE) $(MFLAGS) PROGRAM=$(PPROGRAM) MAINOBJ="pepper_main.$(OBJEXT)" LIBS="$(LIBS) $(PEPPER_LIBS)" CC="$(CXX)" program
+$(PROGRAM_NMF) $(PPROGRAM_NMF): $(@:.nmf=$(EXEEXT)) nacl/create_nmf.rb
+
+.PHONY: pprogram package show_naclflags
+.SUFFIXES: $(EXEEXT) .nmf
+$(EXEEXT).nmf:
+ $(ECHO) generating manifest $@
+ $(Q)$(MINIRUBY) $(srcdir)/nacl/create_nmf.rb --verbose=$(V) $(@:.nmf=$(EXEEXT)) $@
+
+pepper_main.$(OBJEXT): $(srcdir)/nacl/pepper_main.c
+ @$(ECHO) compiling nacl/pepper_main.c
+ $(Q) $(CC) $(CFLAGS) $(XCFLAGS) $(CPPFLAGS) $(COUTFLAG)$@ -c $(srcdir)/nacl/pepper_main.c
+
+.rbconfig.time:
+ @$(MAKE) .rbconfig.raw.time RBCONFIG=.rbconfig.raw.time
+ @sed \
+ -e 's!CONFIG\["CC"\] = .*!CONFIG\["CC"\] = "$(CC)"!' \
+ -e 's!CONFIG\["LD"\] = .*!CONFIG\["LD"\] = "$(LD)"!' \
+ -e 's!CONFIG\["NM"\] = .*!CONFIG\["NM"\] = "$(NM)"!' \
+ -e 's!CONFIG\["AR"\] = .*!CONFIG\["AR"\] = "$(AR)"!' \
+ -e 's!CONFIG\["AS"\] = .*!CONFIG\["AS"\] = "$(AS)"!' \
+ -e 's!CONFIG\["RANLIB"\] = .*!CONFIG\["RANLIB"\] = "$(RANLIB)"!' \
+ -e 's!CONFIG\["OBJDUMP"\] = .*!CONFIG\["OBJDUMP"\] = "$(OBJDUMP)"!' \
+ -e 's!CONFIG\["OBJCOPY"\] = .*!CONFIG\["OBJCOPY"\] = "$(OBJCOPY)"!' \
+ -i.bak rbconfig.rb
+ @touch .rbconfig.time
+
+all: pprogram
+main: $(PROGRAM_NMF)
+pprogram: showflags $(PPROGRAM) $(PPROGRAM_NMF)
+program: $(PROGRAM_NMF)
+prog: pprogram
+
+package: pprogram install-lib install-ext-comm install-ext-arch
+ $(INSTALL_DATA) $(srcdir)/nacl/example.html $(prefix)
+ $(ECHO) generating manifest $@
+ $(Q)$(MINIRUBY) $(srcdir)/nacl/package.rb $(prefix)
+
+showflags: show_naclflags
+
+show_naclflags:
+ @echo " NACL_SDK_ROOT = $(NACL_SDK_ROOT)"
+ @echo " NM = $(NM)"
+ @echo " AR = $(AR)"
+ @echo " AS = $(AS)"
+ @echo " RANLIB = $(RANLIB)"
+ @echo " OBJDUMP = $(OBJDUMP)"
+ @echo " OBJCOPY = $(OBJCOPY)"
+
+clean-local::
+ -$(RM) $(PPROGRAM) pepper_main.$(OBJEXT) $(PROGRAM_NMF) $(PPRGORAM_NMF)
diff --git a/jni/ruby/nacl/README.nacl b/jni/ruby/nacl/README.nacl
new file mode 100644
index 0000000..77140e0
--- /dev/null
+++ b/jni/ruby/nacl/README.nacl
@@ -0,0 +1,51 @@
+=begin
+= Native Client port of Ruby
+
+= How to build
+== Prerequisites
+You need to install the following things before building NaCl port of Ruby.
+* Ruby 1.9.3 or later
+* Python 2.6 or later
+* NativeClient SDK pepper 37 or later
+* GNU make
+
+== Steps
+(1) Extract all files from the tarball:
+ $ tar xzf ruby-X.Y.Z.tar.gz
+(2) Set NACL_SDK_ROOT environment variable to the path to the Native Client SDK you installed:
+ $ export NACL_SDK_ROOT=/home/yugui/src/nacl_sdk/pepper_37
+(3) Configure
+ $ ./configure --prefix=/tmp/nacl-ruby --host=x86_64-nacl --with-baseruby=/path/to/ruby-1.9.3-or-later
+(4) Make
+ $ make
+ $ make package
+
+Now you have ruby.nexe. You can run it by sel_ldr in NaCl SDK.
+
+"make package" installs "pepper-ruby.nexe", an example Pepper application that
+embeds Ruby", and libraries to $prefix. You can run it by the following steps:
+(5) Publish the $prefix directory on a web server
+(6) Visit the example.html on the web server by a browser that implements Pepper 18 or later.
+ -- e.g., Chrome 18 implements Pepper 18, Chrome 19 implements Pepper 19, ...
+
+=== Example Configurations
+* x86_32 Native Client
+ $ ./configure --prefix=/tmp/nacl-ruby \
+ --host=i686-nacl \
+ --with-baseruby=/path/to/ruby-1.9.3-or-later
+* arm Native Client
+ $ ./configure --prefix=/tmp/nacl-ruby \
+ --host=arm-nacl \
+ --with-newlib \
+ --with-baseruby=/path/to/ruby-1.9.3-or-later
+* Portable Native Client
+ $ ./configure --prefix=/tmp/nacl-ruby \
+ --host=le32-nacl \
+ --with-newlib \
+ --with-static-linked-ext \
+ --with-baseruby=/path/to/ruby-1.9.3-or-later
+
+= Copyright
+* Copyright 2012 Google Inc. All Rights Reserved.
+* Author: yugui@google.com (Yugui Sonoda)
+=end
diff --git a/jni/ruby/nacl/create_nmf.rb b/jni/ruby/nacl/create_nmf.rb
new file mode 100644
index 0000000..cdfe7ad
--- /dev/null
+++ b/jni/ruby/nacl/create_nmf.rb
@@ -0,0 +1,70 @@
+#!/usr/bin/ruby
+# Copyright:: Copyright 2012 Google Inc.
+# License:: All Rights Reserved.
+# Original Author:: Yugui Sonoda (mailto:yugui@google.com)
+#
+# Wrapper for create_nmf.py / generate_nmf.py
+
+require File.join(File.dirname(__FILE__), 'nacl-config')
+
+include NaClConfig
+$verbosity = 0
+
+def usage_and_exit
+ $stderr.puts "Usage: #{$PROGRAM_NAME} [--verbose=N] path/to/input.nexe path/to/output.nmf"
+ exit false
+end
+
+def create_dynamically_linked(nmf, exe)
+ cmd = [
+ PYTHON, CREATE_NMF,
+ '-o', nmf,
+ '-D', OBJDUMP,
+ '-L', HOST_LIB,
+ exe
+ ]
+ puts cmd.join(' ') if $verbosity > 0
+ exec(*cmd)
+end
+
+def create_statically_linked(nmf, exe)
+ File.open(nmf, "w") {|f|
+ f.write <<-EOS.gsub(/^ {6}/, '')
+ {
+ "program": {
+ "#{ARCH}": {
+ "url": "#{exe}"
+ }
+ }
+ }
+ EOS
+ }
+end
+
+def main
+ while m = ARGV.first.match(/--([a-z-]+)(?:=(\S+))?/)
+ case m[1]
+ when 'verbose'
+ usage_and_exit unless m[2][/\A[0-9]+\z/]
+ $verbosity = m[2].to_i
+ when 'help'
+ usage_end_exit
+ end
+ ARGV.shift
+ end
+
+ usage_and_exit if ARGV.size < 2
+
+ exe, nmf = ARGV[0], ARGV[1]
+ if newlib?
+ create_statically_linked(nmf, exe)
+ else
+ create_dynamically_linked(nmf, exe)
+ end
+end
+
+if __FILE__ == $0
+ main()
+end
+
+
diff --git a/jni/ruby/nacl/dirent.h b/jni/ruby/nacl/dirent.h
new file mode 100644
index 0000000..31bdad3
--- /dev/null
+++ b/jni/ruby/nacl/dirent.h
@@ -0,0 +1,15 @@
+/*
+ * Copyright 2011 Google Inc. All Rights Reserved.
+ * Author: yugui@google.com (Yugui Sonoda)
+ */
+#ifndef RUBY_NACL_DIRENT_H
+#define RUBY_NACL_DIRENT_H
+
+/* NaCl SDK 0.3 has implementations of dir functions but no declaration in
+ * dirent.h */
+int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result);
+void rewinddir(DIR *dirp);
+long telldir(DIR *dirp);
+void seekdir(DIR *dirp, long offset);
+
+#endif
diff --git a/jni/ruby/nacl/example.html b/jni/ruby/nacl/example.html
new file mode 100644
index 0000000..3cc3329
--- /dev/null
+++ b/jni/ruby/nacl/example.html
@@ -0,0 +1,150 @@
+
+
+
+ Ruby Example
+
+
+
+
+
+Native Client Module Ruby
+
+
+
+
+
+Status
+NO-STATUS
+
+
diff --git a/jni/ruby/nacl/nacl-config.rb b/jni/ruby/nacl/nacl-config.rb
new file mode 100755
index 0000000..6648130
--- /dev/null
+++ b/jni/ruby/nacl/nacl-config.rb
@@ -0,0 +1,61 @@
+#!/usr/bin/ruby
+#
+# Copyright:: Copyright 2012 Google Inc.
+# License:: All Rights Reserved.
+# Original Author:: Yugui Sonoda (mailto:yugui@google.com)
+#
+# Convenient functions/constants for native client specific configurations.
+require 'rbconfig'
+
+module NaClConfig
+ config = RbConfig::CONFIG
+
+ cpu_nick = config['host_alias'].sub(/-gnu$|-newlib$/, '').sub(/-nacl$/, '').sub(/i.86/, 'x86_32')
+ ARCH = cpu_nick.sub('x86_64', 'x86-64').sub('x86_32', 'x86-32')
+ HOST = ARCH.sub(/x86-../, 'x86_64') + '-nacl'
+
+ lib_suffix = config['host_cpu'][/i.86/] ? '32' : ''
+ PYTHON = config['PYTHON']
+ OBJDUMP = config['OBJDUMP']
+ SDK_ROOT = config['NACL_SDK_ROOT']
+ CREATE_NMF = [
+ File.join(SDK_ROOT, 'build_tools', 'nacl_sdk_scons', 'site_tools', 'create_nmf.py'),
+ File.join(SDK_ROOT, 'tools', 'create_nmf.py')
+ ].find{|path| File.exist?(path) } or raise "No create_nmf found"
+ HOST_LIB = File.join(SDK_ROOT, 'toolchain', config['NACL_TOOLCHAIN'], HOST, "lib#{lib_suffix}")
+
+ NACL_LIB = File.join(SDK_ROOT, 'lib', config['NACL_LIB_PATH'], 'Release')
+
+ INSTALL_PROGRAM = config['INSTALL_PROGRAM']
+ INSTALL_LIBRARY = config['INSTALL_DATA']
+
+ if cpu_nick == 'x86_64' or cpu_nick == 'x86_32'
+ SEL_LDR = File.join(SDK_ROOT, 'tools', "sel_ldr_#{cpu_nick}")
+ IRT_CORE = File.join(SDK_ROOT, 'tools', "irt_core_#{cpu_nick}.nexe")
+ raise "No sel_ldr found" if not File.executable?(SEL_LDR)
+ raise "No irt_core found" if not File.exists?(IRT_CORE)
+ end
+ RUNNABLE_LD = File.join(HOST_LIB, 'runnable-ld.so')
+
+ module_function
+
+ def newlib?
+ RbConfig::CONFIG['NACL_SDK_VARIANT'] == 'newlib'
+ end
+
+ def self.config(name)
+ if NaClConfig::const_defined?(name.upcase)
+ NaClConfig::const_get(name.upcase)
+ elsif NaClConfig::respond_to?(name) and NaClConfig::method(name).arity == 0
+ NaClConfig::send(name)
+ else
+ raise ArgumentError, "No such config: #{name}"
+ end
+ end
+end
+
+if $0 == __FILE__
+ ARGV.each do |arg|
+ puts NaClConfig::config(arg)
+ end
+end
diff --git a/jni/ruby/nacl/package.rb b/jni/ruby/nacl/package.rb
new file mode 100644
index 0000000..9ba9588
--- /dev/null
+++ b/jni/ruby/nacl/package.rb
@@ -0,0 +1,113 @@
+#!/usr/bin/ruby
+# Copyright:: Copyright 2012 Google Inc.
+# License:: All Rights Reserved.
+# Original Author:: Yugui Sonoda (mailto:yugui@google.com)
+#
+# Generates a runnable package of the pepper API example.
+
+require File.join(File.dirname(__FILE__), 'nacl-config')
+require 'json'
+require 'find'
+require 'fileutils'
+
+include NaClConfig
+
+class Installation
+ include NaClConfig
+
+ SRC_DIRS = [
+ Dir.pwd,
+ HOST_LIB,
+ NACL_LIB,
+ ]
+
+ def initialize(destdir)
+ @destdir = destdir
+ @manifest = {
+ "files" => {}
+ }
+ ruby_libs.each do |path|
+ raise "Collision of #{path}" if @manifest['files'].key? path
+ @manifest['files'][path] = {
+ ARCH => {
+ "url" => path
+ }
+ }
+ if path[/\.so$/]
+ alternate_path = path.gsub('/', "_")
+ raise "Collision of #{alternate_path}" if @manifest['files'].key? alternate_path
+ @manifest['files'][alternate_path] = {
+ ARCH => {
+ "url" => path
+ }
+ }
+ end
+ end
+ end
+
+ def manifest
+ @manifest.dup
+ end
+
+ def install_program(basename)
+ do_install_binary(basename, File.join(@destdir, "bin", ARCH))
+ @manifest["program"] = {
+ ARCH => {
+ "url" => File.join("bin", ARCH, basename)
+ }
+ }
+ end
+
+ def install_library(name, basename)
+ do_install_binary(basename, File.join(@destdir, "lib", ARCH))
+ @manifest["files"][name] = {
+ ARCH => {
+ "url" => File.join("lib", ARCH, basename)
+ }
+ }
+ end
+
+ private
+ def do_install_binary(basename, dest_dir)
+ full_path = nil
+ catch(:found) {
+ SRC_DIRS.each do |path|
+ full_path = File.join(path, basename)
+ if File.exist? full_path
+ throw :found
+ end
+ end
+ raise Errno::ENOENT, "No such file to install: %s" % basename
+ }
+ FileUtils.mkdir_p dest_dir
+ system("#{INSTALL_PROGRAM} #{full_path} #{dest_dir}")
+ end
+
+ def ruby_libs
+ Find.find(RbConfig::CONFIG['rubylibdir']).select{|path| File.file?(path) }.map{|path| path.sub("#{@destdir}/", "") }
+ end
+end
+
+def install(destdir)
+ inst = Installation.new(destdir)
+ manifest = JSON.parse(File.read("pepper-ruby.nmf"))
+
+ program = File.basename(manifest['program'][ARCH]['url'])
+ inst.install_program(program)
+
+ manifest['files'].each do |name, attr|
+ inst.install_library(name, File.basename(attr[ARCH]["url"]))
+ end
+
+ File.open(File.join(destdir, "ruby.nmf"), "w") {|f|
+ f.puts JSON.pretty_generate(inst.manifest)
+ }
+end
+
+def main
+ install(ARGV[0])
+end
+
+if __FILE__ == $0
+ main()
+end
diff --git a/jni/ruby/nacl/pepper_main.c b/jni/ruby/nacl/pepper_main.c
new file mode 100644
index 0000000..168e8f9
--- /dev/null
+++ b/jni/ruby/nacl/pepper_main.c
@@ -0,0 +1,732 @@
+/******************************************************************************
+ Copyright 2012 Google Inc. All Rights Reserved.
+ Author: yugui@google.com (Yugui Sonoda)
+ ******************************************************************************/
+
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include "ppapi/c/pp_errors.h"
+#include "ppapi/c/pp_module.h"
+#include "ppapi/c/pp_var.h"
+#include "ppapi/c/ppb.h"
+#include "ppapi/c/ppb_core.h"
+#include "ppapi/c/ppb_file_ref.h"
+#include "ppapi/c/ppb_instance.h"
+#include "ppapi/c/ppb_messaging.h"
+#include "ppapi/c/ppb_url_loader.h"
+#include "ppapi/c/ppb_url_request_info.h"
+#include "ppapi/c/ppb_url_response_info.h"
+#include "ppapi/c/ppb_var.h"
+#include "ppapi/c/ppp.h"
+#include "ppapi/c/ppp_instance.h"
+#include "ppapi/c/ppp_messaging.h"
+#include "nacl_io/nacl_io.h"
+
+#include "verconf.h"
+#include "ruby/ruby.h"
+#include "version.h"
+#include "gc.h"
+
+#ifdef HAVE_STRUCT_PPB_CORE
+typedef struct PPB_Core PPB_Core;
+#endif
+#ifdef HAVE_STRUCT_PPB_MESSAGING
+typedef struct PPB_Messaging PPB_Messaging;
+#endif
+#ifdef HAVE_STRUCT_PPB_VAR
+typedef struct PPB_Var PPB_Var;
+#endif
+#ifdef HAVE_STRUCT_PPB_URLLOADER
+typedef struct PPB_URLLoader PPB_URLLoader;
+#endif
+#ifdef HAVE_STRUCT_PPB_URLREQUESTINFO
+typedef struct PPB_URLRequestInfo PPB_URLRequestInfo;
+#endif
+#ifdef HAVE_STRUCT_PPB_URLRESPONSEINFO
+typedef struct PPB_URLResponseInfo PPB_URLResponseInfo;
+#endif
+#ifdef HAVE_STRUCT_PPP_INSTANCE
+typedef struct PPP_Instance PPP_Instance;
+#endif
+
+static PP_Module module_id = 0;
+static PPB_GetInterface get_browser_interface = NULL;
+static PPB_Core* core_interface = NULL;
+static PPB_Messaging* messaging_interface = NULL;
+static PPB_Var* var_interface = NULL;
+static PPB_URLLoader* loader_interface = NULL;
+static PPB_URLRequestInfo* request_interface = NULL;
+static PPB_URLResponseInfo* response_interface = NULL;
+static PPB_FileRef* fileref_interface = NULL;
+static struct st_table* instance_data = NULL;
+
+static VALUE instance_table = Qundef;
+
+static PP_Instance current_instance = 0;
+
+/******************************************************************************
+ * State of instance
+ ******************************************************************************/
+
+static void inst_mark(void *const ptr);
+static void inst_free(void *const ptr);
+static size_t inst_memsize(void *const ptr);
+static const rb_data_type_t pepper_instance_data_type = {
+ "PepperInstance",
+ { inst_mark, inst_free, inst_memsize }
+};
+
+struct PepperInstance {
+ PP_Instance instance;
+ PP_Resource url_loader;
+ VALUE self;
+ void* async_call_args;
+ union {
+ int32_t as_int;
+ const char* as_str;
+ VALUE as_value;
+ } async_call_result;
+ char buf[1000];
+
+ pthread_t th;
+ pthread_mutex_t mutex;
+ pthread_cond_t cond;
+};
+
+struct PepperInstance*
+pruby_get_instance(PP_Instance instance)
+{
+ VALUE self = rb_hash_aref(instance_table, INT2FIX(instance));
+ if (RTEST(self)) {
+ struct PepperInstance *inst;
+ TypedData_Get_Struct(self, struct PepperInstance, &pepper_instance_data_type, inst);
+ return inst;
+ }
+ else {
+ return NULL;
+ }
+}
+
+#define GET_PEPPER_INSTANCE() (pruby_get_instance(current_instance))
+
+struct PepperInstance*
+pruby_register_instance(PP_Instance instance)
+{
+ VALUE obj;
+ struct PepperInstance *data;
+ obj = TypedData_Make_Struct(rb_cData, struct PepperInstance, &pepper_instance_data_type, data);
+ data->self = obj;
+ data->instance = instance;
+ data->url_loader = 0;
+
+ pthread_mutex_init(&data->mutex, NULL);
+ pthread_cond_init(&data->cond, NULL);
+
+ rb_hash_aset(instance_table, INT2FIX(instance), obj);
+ return data;
+}
+
+int
+pruby_unregister_instance(PP_Instance instance)
+{
+ VALUE inst = rb_hash_delete(instance_table, INT2FIX(instance));
+ return RTEST(inst);
+}
+
+static void
+inst_mark(void *const ptr)
+{
+ RUBY_MARK_ENTER("PepperInstance"0);
+ if (ptr) {
+ const struct PepperInstance* inst = (struct PepperInstance*)ptr;
+ RUBY_MARK_UNLESS_NULL(inst->async_call_result.as_value);
+ }
+ RUBY_MARK_LEAVE("PepperInstance"0);
+}
+
+static void
+inst_free(void *const ptr)
+{
+ ruby_xfree(ptr);
+}
+
+static size_t
+inst_memsize(void *const ptr)
+{
+ if (ptr) {
+ const struct PepperInstance* inst = (struct PepperInstance*)ptr;
+ return sizeof(*inst);
+ } else {
+ return 0;
+ }
+}
+
+void
+pruby_async_return_int(void* data, int32_t result)
+{
+ /* PPAPI main thread */
+ struct PepperInstance* const instance = (struct PepperInstance*)data;
+ instance->async_call_result.as_int = result;
+ if (pthread_cond_signal(&instance->cond)) {
+ perror("pepper-ruby:pthread_cond_signal");
+ }
+}
+
+void
+pruby_async_return_str(void* data, const char *result)
+{
+ /* PPAPI main thread */
+ struct PepperInstance* const instance = (struct PepperInstance*)data;
+ instance->async_call_result.as_str = result;
+ if (pthread_cond_signal(&instance->cond)) {
+ perror("pepper-ruby:pthread_cond_signal");
+ }
+}
+
+void
+pruby_async_return_value(void* data, VALUE value)
+{
+ /* PPAPI main thread */
+ struct PepperInstance* const instance = (struct PepperInstance*)data;
+ instance->async_call_result.as_value = value;
+ if (pthread_cond_signal(&instance->cond)) {
+ perror("pepper-ruby:pthread_cond_signal");
+ }
+}
+/******************************************************************************
+ * Conversion between Ruby's VALUE, Pepper's Var and C string
+ ******************************************************************************/
+
+/**
+ * Creates a new string PP_Var from C string. The resulting object will be a
+ * refcounted string object. It will be AddRef()ed for the caller. When the
+ * caller is done with it, it should be Release()d.
+ * @param[in] str C string to be converted to PP_Var
+ * @return PP_Var containing string.
+ */
+static struct PP_Var
+pruby_cstr_to_var(const char* str)
+{
+#ifndef PPB_VAR_INTERFACE_1_1
+ if (var_interface != NULL)
+ return var_interface->VarFromUtf8(module_id, str, strlen(str));
+ return PP_MakeUndefined();
+#else
+ return var_interface->VarFromUtf8(str, strlen(str));
+#endif
+}
+
+/**
+ * Returns a mutable C string contained in the @a var or NULL if @a var is not
+ * string. This makes a copy of the string in the @a var and adds a NULL
+ * terminator. Note that VarToUtf8() does not guarantee the NULL terminator on
+ * the returned string. See the comments for VarToUtf8() in ppapi/c/ppb_var.h
+ * for more info. The caller is responsible for freeing the returned memory.
+ * @param[in] var PP_Var containing string.
+ * @return a mutable C string representation of @a var.
+ * @note The caller is responsible for freeing the returned string.
+ */
+static char*
+pruby_var_to_cstr(struct PP_Var var)
+{
+ uint32_t len = 0;
+ if (var_interface != NULL) {
+ const char* var_c_str = var_interface->VarToUtf8(var, &len);
+ if (len > 0) {
+ char* c_str = (char*)malloc(len + 1);
+ memcpy(c_str, var_c_str, len);
+ c_str[len] = '\0';
+ return c_str;
+ }
+ }
+ return NULL;
+}
+
+static struct PP_Var
+pruby_str_to_var(volatile VALUE str)
+{
+ if (!RB_TYPE_P(str, T_STRING)) {
+ fprintf(stderr, "[BUG] Unexpected object type: %x\n", TYPE(str));
+ exit(EXIT_FAILURE);
+ }
+#ifndef PPB_VAR_INTERFACE_1_1
+ if (var_interface != NULL) {
+ return var_interface->VarFromUtf8(module_id, RSTRING_PTR(str), RSTRING_LEN(str));
+ }
+#else
+ return var_interface->VarFromUtf8(RSTRING_PTR(str), RSTRING_LEN(str));
+#endif
+ return PP_MakeUndefined();
+}
+
+static struct PP_Var
+pruby_obj_to_var(volatile VALUE obj)
+{
+ static const char* const error =
+ "throw 'Failed to convert the result to a JavaScript object';";
+ int state;
+ obj = rb_protect(&rb_obj_as_string, obj, &state);
+ if (!state) {
+ return pruby_str_to_var(obj);
+ }
+ else {
+ return pruby_cstr_to_var(error);
+ }
+}
+
+int
+pruby_var_equal_to_cstr_p(struct PP_Var lhs, const char* rhs)
+{
+ uint32_t len = 0;
+ if (var_interface == NULL) {
+ return 0;
+ }
+ else {
+ const char* const cstr = var_interface->VarToUtf8(lhs, &len);
+ return strncmp(cstr, rhs, len) == 0;
+ }
+}
+
+int
+pruby_var_prefixed_p(struct PP_Var var, const char* prefix)
+{
+ uint32_t len = 0;
+ if (var_interface == NULL) {
+ return 0;
+ }
+ else {
+ const char* const cstr = var_interface->VarToUtf8(var, &len);
+ const size_t prefix_len = strlen(prefix);
+ return len >= prefix_len && memcmp(cstr, prefix, len) == 0;
+ }
+}
+
+
+/******************************************************************************
+ * Messaging
+ ******************************************************************************/
+
+/* Posts the given C string as a message.
+ * @param data pointer to a NULL-terminated string */
+void
+pruby_post_cstr(void* data)
+{
+ /* PPAPI main thread */
+ struct PepperInstance* const instance = (struct PepperInstance*)data;
+ const char* const msg = (const char*)instance->async_call_args;
+ messaging_interface->PostMessage(instance->instance,
+ pruby_cstr_to_var(msg));
+}
+
+/* Posts the given Ruby VALUE as a message.
+ * @param data a VALUE casted to void* */
+void
+pruby_post_value(void* data)
+{
+ /* PPAPI main thread */
+ struct PepperInstance* const instance = (struct PepperInstance*)data;
+ volatile VALUE value = (VALUE)instance->async_call_args;
+ messaging_interface->PostMessage(instance->instance, pruby_obj_to_var(value));
+}
+
+
+
+/******************************************************************************
+ * Ruby initialization
+ ******************************************************************************/
+
+static void
+init_loadpath(void)
+{
+ ruby_incpush("lib/ruby/"RUBY_LIB_VERSION);
+ ruby_incpush("lib/ruby/"RUBY_LIB_VERSION"/"RUBY_PLATFORM);
+ ruby_incpush(".");
+}
+
+static VALUE
+init_libraries_internal(VALUE unused)
+{
+ extern void Init_enc();
+ extern void Init_ext();
+
+ init_loadpath();
+ Init_enc();
+ Init_ext();
+ return Qnil;
+}
+
+static void*
+init_libraries(void* data)
+{
+ int state;
+ struct PepperInstance* const instance = (struct PepperInstance*)data;
+ current_instance = instance->instance;
+
+ if (pthread_mutex_lock(&instance->mutex)) {
+ perror("pepper-ruby:pthread_mutex_lock");
+ return 0;
+ }
+ rb_protect(&init_libraries_internal, Qnil, &state);
+ pthread_mutex_unlock(&instance->mutex);
+
+ if (state) {
+ volatile VALUE err = rb_errinfo();
+ err = rb_obj_as_string(err);
+ } else {
+ instance->async_call_args = (void*)"rubyReady";
+ core_interface->CallOnMainThread(
+ 0, PP_MakeCompletionCallback(pruby_post_cstr, instance), 0);
+ }
+ return NULL;
+}
+
+static int
+init_libraries_if_necessary(void)
+{
+ static int initialized = 0;
+ if (!initialized) {
+ struct PepperInstance* const instance = GET_PEPPER_INSTANCE();
+ int err;
+ initialized = 1;
+ err = pthread_create(&instance->th, NULL, &init_libraries, instance);
+ if (err) {
+ fprintf(stderr, "pepper_ruby:pthread_create: %s\n", strerror(err));
+ exit(EXIT_FAILURE);
+ }
+ pthread_detach(instance->th);
+ }
+ return 0;
+}
+
+static int
+reopen_fd(int fd, const char* path, int flags) {
+ int fd2 = open(path, flags);
+ if (fd2 < 0) {
+ perror("open fd");
+ return -1;
+ }
+ if (dup2(fd2, fd) < 0) {
+ perror("dup2 fd");
+ return -1;
+ }
+ if (close(fd2)) {
+ perror("close old fd");
+ return -1;
+ }
+ return fd;
+}
+
+static int
+pruby_init(void)
+{
+ RUBY_INIT_STACK;
+ ruby_init();
+
+ instance_table = rb_hash_new();
+ rb_gc_register_mark_object(instance_table);
+
+ return 0;
+}
+
+
+/******************************************************************************
+ * Ruby evaluation
+ ******************************************************************************/
+
+static void*
+pruby_eval(void* data)
+{
+ extern VALUE ruby_eval_string_from_file_protect(const char* src, const char* path, int* state);
+ struct PepperInstance* const instance = (struct PepperInstance*)data;
+ volatile VALUE src = (VALUE)instance->async_call_args;
+ volatile VALUE result = Qnil;
+ volatile int state;
+
+ RUBY_INIT_STACK;
+
+ if (pthread_mutex_lock(&instance->mutex)) {
+ perror("pepper-ruby:pthread_mutex_lock");
+ return 0;
+ }
+ result = ruby_eval_string_from_file_protect(
+ RSTRING_PTR(src), "(pepper-ruby)", &state);
+ pthread_mutex_unlock(&instance->mutex);
+
+ if (!state) {
+ instance->async_call_args =
+ rb_str_concat(rb_usascii_str_new_cstr("return:"),
+ rb_obj_as_string(result));
+ core_interface->CallOnMainThread(
+ 0, PP_MakeCompletionCallback(pruby_post_value, instance), 0);
+ return NULL;
+ }
+ else {
+ rb_set_errinfo(Qnil);
+ instance->async_call_args =
+ rb_str_concat(rb_usascii_str_new_cstr("error:"),
+ rb_obj_as_string(result));
+ core_interface->CallOnMainThread(
+ 0, PP_MakeCompletionCallback(pruby_post_value, instance), 0);
+ return NULL;
+ }
+}
+
+
+/******************************************************************************
+ * Pepper Module callbacks
+ ******************************************************************************/
+
+/**
+ * Called when the NaCl module is instantiated on the web page. The identifier
+ * of the new instance will be passed in as the first argument (this value is
+ * generated by the browser and is an opaque handle). This is called for each
+ * instantiation of the NaCl module, which is each time the