public static IRubyObject validateStringEncoding(ThreadContext context, Descriptors.FieldDescriptor.Type type, IRubyObject value) { if (!(value instanceof RubyString)) throw context.runtime.newTypeError("Invalid argument for string field."); switch(type) { case BYTES: value = ((RubyString)value).encode(context, context.runtime.evalScriptlet("Encoding::ASCII_8BIT")); break; case STRING: value = ((RubyString)value).encode(context, context.runtime.evalScriptlet("Encoding::UTF_8")); break; default: break; } value.setFrozen(true); return value; }
/** * Evaluate a script and return the last value in the script. * @param runtime to invoke the script under * @param script to be evaluated * @return the last value of the script */ public IRubyObject eval(Ruby runtime, String script) { return runtime.evalScriptlet(script); }
/** * Evaluate a script and return the last value in the script. * @param runtime to invoke the script under * @param script to be evaluated * @return the last value of the script */ public IRubyObject eval(Ruby runtime, String script) { return runtime.evalScriptlet(script); }
@Override public IRubyObject createApplicationObject(Ruby runtime) { runtime.evalScriptlet("load 'jruby/rack/boot/rails.rb'"); runtime.evalScriptlet("JRuby::Rack::RailsBooter.load_environment"); return createRackServletWrapper(runtime, "run JRuby::Rack::RailsBooter.to_app"); } }
void inject(Map<String, Object> environmentVars) { Set<Entry<String, Object>> environmentVariablesAndValues = environmentVars.entrySet(); for (Entry<String, Object> entry : environmentVariablesAndValues) { runtime.evalScriptlet(String.format("ENV['%s']=\"%s\"", entry.getKey(), entry.getValue())); } }
public void includeProcessor( Class<? extends IncludeProcessor> includeProcessor) { // this may change in future to external class to deal with dynamic // imports this.rubyRuntime.evalScriptlet("java_import " + includeProcessor.getName()); this.asciidoctorModule.include_processor(includeProcessor .getSimpleName()); }
public void preprocessor(Class<? extends Preprocessor> preprocessor) { // this may change in future to external class to deal with dynamic // imports this.rubyRuntime.evalScriptlet("java_import " + preprocessor.getName()); this.asciidoctorModule.preprocessor(preprocessor.getSimpleName()); }
public void inlineMacro(String blockName, Class<? extends InlineMacroProcessor> inlineMacroProcessor) { // this may change in future to external class to deal with dynamic // imports this.rubyRuntime.evalScriptlet("java_import " + inlineMacroProcessor.getName()); this.asciidoctorModule.inline_macro( RubyUtils.toSymbol(rubyRuntime, blockName), inlineMacroProcessor.getSimpleName()); }
public void blockMacro(String blockName, Class<? extends BlockMacroProcessor> blockMacroProcessor) { // this may change in future to external class to deal with dynamic // imports this.rubyRuntime.evalScriptlet("java_import " + blockMacroProcessor.getName()); this.asciidoctorModule.block_macro( RubyUtils.toSymbol(rubyRuntime, blockName), blockMacroProcessor.getSimpleName()); }
private JRubyAsciidoctor(final Ruby rubyRuntime) { this.rubyRuntime = rubyRuntime; InputStream inputStream = getClass().getResourceAsStream("asciidoctorclass.rb"); final String script = IOUtils.readFull(inputStream); this.rubyRuntime.evalScriptlet(script); this.rubyGemsPreloader = new RubyGemsPreloader(this.rubyRuntime); this.logHandlers.add(new JULLogHandler()); }
private JRubyAsciidoctor(final Ruby rubyRuntime) { this.rubyRuntime = rubyRuntime; InputStream inputStream = getClass().getResourceAsStream("asciidoctorclass.rb"); final String script = IOUtils.readFull(inputStream); this.rubyRuntime.evalScriptlet(script); this.rubyGemsPreloader = new RubyGemsPreloader(this.rubyRuntime); this.logHandlers.add(new JULLogHandler()); }
private void setTimingsMode(Asciidoctor asciidoctor, AsciidoctorCliOptions asciidoctorCliOptions, Options options) { if (asciidoctorCliOptions.isTimings()) { options.setOption("timings", JRubyRuntimeContext.get(asciidoctor).evalScriptlet("Asciidoctor::Timings.new")); } }
private void setTimingsMode(Asciidoctor asciidoctor, AsciidoctorCliOptions asciidoctorCliOptions, Options options) { if (asciidoctorCliOptions.isTimings()) { options.setOption("timings", JRubyRuntimeContext.get(asciidoctor).evalScriptlet("Asciidoctor::Timings.new")); } }
public IRubyObject createApplicationObject(final Ruby runtime) { if (rackupScript == null) { rackContext.log(RackLogger.WARN, "no rackup script found - starting empty Rack application!"); rackupScript = ""; } checkAndSetRackVersion(runtime); runtime.evalScriptlet("load 'jruby/rack/boot/rack.rb'"); return createRackServletWrapper(runtime, rackupScript, rackupLocation); }
@Test public void should_not_have_gempath_in_ruby_env_when_created_with_null_gempath() { // Given: Our environment is polluted (Cannot set these env vars here, so just check that gradle has set them correctly) assertThat(System.getenv("GEM_PATH"), notNullValue()); assertThat(System.getenv("GEM_HOME"), notNullValue()); // When: A new Asciidoctor instance is created passing in a null GEM_PATH Asciidoctor asciidoctor = Asciidoctor.Factory.create((String) null); // Then: The org.jruby.JRuby instance does not see this variable Ruby rubyRuntime = JRubyRuntimeContext.get(asciidoctor); assertThat(rubyRuntime.evalScriptlet("ENV['GEM_PATH']"), is(rubyRuntime.getNil())); assertThat(rubyRuntime.evalScriptlet("ENV['GEM_HOME']"), is(rubyRuntime.getNil())); }
@Test public void should_have_gempath_in_ruby_env_when_created_with_default_create() { // Given: Our environment is polluted (Cannot set these env vars here, so just check that gradle has set them correctly) assertThat(System.getenv("GEM_PATH"), notNullValue()); assertThat(System.getenv("GEM_HOME"), notNullValue()); // When: A new Asciidoctor instance is created passing in no GEM_PATH Asciidoctor asciidoctor = Asciidoctor.Factory.create(); // Then: The org.jruby.JRuby instance sees this variable Ruby rubyRuntime = JRubyRuntimeContext.get(asciidoctor); assertThat(rubyRuntime.evalScriptlet("ENV['GEM_PATH']"), is(rubyRuntime.getNil())); assertThat(rubyRuntime.evalScriptlet("ENV['GEM_HOME']"), is(rubyRuntime.getNil())); }
@Test public void erubis_gem_should_be_preloaded() { Map<String, Object> options = OptionsBuilder.options().eruby("erubis").asMap(); ((JRubyAsciidoctor) asciidoctor).rubyGemsPreloader.preloadRequiredLibraries(options); RubyBoolean evalScriptlet = (RubyBoolean) ((JRubyAsciidoctor) asciidoctor).rubyRuntime.evalScriptlet("require 'erubis'"); assertThat(evalScriptlet.isFalse(), is(true)); }
@Test public void not_erubis_gem_should_be_preloaded() { Map<String, Object> options = OptionsBuilder.options().eruby("erb").asMap(); ((JRubyAsciidoctor) asciidoctor).rubyGemsPreloader.preloadRequiredLibraries(options); RubyBoolean evalScriptlet = (RubyBoolean) ((JRubyAsciidoctor) asciidoctor).rubyRuntime.evalScriptlet("require 'erubis'"); assertThat(evalScriptlet.isTrue(), is(true)); }
@Test public void data_uri_gem_should_be_preloaded() { Map<String, Object> options = OptionsBuilder.options() .attributes(AttributesBuilder.attributes().dataUri(true).get()).asMap(); ((JRubyAsciidoctor) asciidoctor).rubyGemsPreloader.preloadRequiredLibraries(options); RubyBoolean evalScriptlet = (RubyBoolean) ((JRubyAsciidoctor) asciidoctor).rubyRuntime.evalScriptlet("require 'base64'"); assertThat(evalScriptlet.isFalse(), is(true)); }
@Test public void not_coderay_gem_should_not_be_preloaded() { Map<String, Object> options = OptionsBuilder.options() .attributes(AttributesBuilder.attributes().sourceHighlighter("pygments").get()).asMap(); ((JRubyAsciidoctor) asciidoctor).rubyGemsPreloader.preloadRequiredLibraries(options); RubyBoolean evalScriptlet = (RubyBoolean) ((JRubyAsciidoctor) asciidoctor).rubyRuntime.evalScriptlet("require 'coderay'"); assertThat(evalScriptlet.isTrue(), is(true)); }