Add some demo scripts and crap like that

This commit is contained in:
R. Tyler Croy 2015-07-25 13:38:22 -07:00
parent a56e81bec8
commit f9dfd1aa80
No known key found for this signature in database
GPG Key ID: 1426C7DC3F51E16F
7 changed files with 120 additions and 0 deletions

49
build.gradle Normal file
View File

@ -0,0 +1,49 @@
buildscript {
repositories { jcenter() }
dependencies {
classpath "com.github.jruby-gradle:jruby-gradle-plugin:0.4.0"
}
}
apply plugin: 'com.github.jruby-gradle.base'
import com.github.jrubygradle.JRubyExec
defaultTasks 'runServer'
/* Disabling the default repositories so we can ues the new (unreleased)
* default rubygems proxy
*/
jruby { defaultRepositories = false }
repositories {
maven { url "http://rubygems.lasagna.io/proxy/maven/releases" }
jcenter()
/* Needed to pull in avatar-js */
maven { url "https://maven.java.net/content/repositories/snapshots" }
}
configurations {
avatarjs
}
dependencies {
jrubyExec "com.oracle:avatar-js:${avatarJsVersion}"
avatarjs "com.oracle:libavatar-js-linux-x64:${avatarJsVersion}"
}
task prepareNative(type: Copy) {
from configurations.avatarjs
rename "libavatar-js-linux-x64-0.10.31-SNAPSHOT.so", "libavatar-js.so"
into "${buildDir}/native"
}
task runDemo(type: JRubyExec) {
script './demo.rb'
}
task runServer(type: JRubyExec) {
script './server.rb'
systemProperties 'java.library.path' : "${buildDir}/native"
dependsOn prepareNative
}

3
demo.js Normal file
View File

@ -0,0 +1,3 @@
var addThing = function(left, right) {
return left + right;
};

35
demo.rb Normal file
View File

@ -0,0 +1,35 @@
#!/usr/bin/env ruby
require 'java'
java_import 'javax.script.ScriptEngineManager'
# This JS class is a simple proxy class to help dispatch JavaScript function
# calls from JRuby into an instantiated Nashorn JS engine
class JS
# Instantiate the engine, when passed a file_path, set up the engine and
# evaluate the file_path
def initialize(file_path=nil)
@engine = ScriptEngineManager.new.get_engine_by_name('nashorn')
if file_path
self.eval_file(file_path)
end
end
# Evaluate the JavaScript at the given file_path via the JS engine
def eval_file(file_path)
@engine.eval(File.open(file_path, 'r').read)
end
# Defining method_missing so we can dispatch methods into our Nashorn engine
# if we receive a functional call destined for JS
def method_missing(method, *args)
return @engine.invoke_function(method.to_s, *args)
end
end
puts '-' * 30
js = JS.new 'demo.js'
result = js.addThing(1, 2)
puts "Doing with math in JavaScript gives us: #{result}"
puts '-' * 30

2
gradle.properties Normal file
View File

@ -0,0 +1,2 @@
org.gradle.daemon=true
avatarJsVersion=0.10.31-SNAPSHOT

9
gradle.rb Executable file
View File

@ -0,0 +1,9 @@
#!/bin/sh
## Hack our GEM_HOME to make sure that the `rubygems` support can find our
# unpacked gems in the given GEMFOLDER
export GEM_HOME="/home/tyler/source/github/ruby/nashorn-ruby/build/gems"
export GEM_PATH="/home/tyler/source/github/ruby/nashorn-ruby/build/gems"
exec java -cp /home/tyler/.gradle/caches/modules-2/files-2.1/org.jruby/jruby-complete/1.7.21/e061b9f399a5e8d5cfcca84d4a6baf879111e83c/jruby-complete-1.7.21.jar org.jruby.Main -S $@

14
helloserv.js Normal file
View File

@ -0,0 +1,14 @@
// Load the http module to create an http server.
var http = require('http');
// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("Hello World\n");
});
// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(8000);
// Put a friendly message on the terminal
console.log("Server running at http://127.0.0.1:8000/");

8
server.rb Normal file
View File

@ -0,0 +1,8 @@
#!/usr/bin/env ruby
require 'java'
java_import 'com.oracle.avatar.js.Server'
server = Server.new
puts server.inspect
server.run('helloserv.js')