Permalink
Browse files

first pass

  • Loading branch information...
defunkt committed Jun 19, 2009
0 parents commit e889787684b03578218df27b8f16792abef09365
Showing with 153 additions and 0 deletions.
  1. +20 −0 LICENSE
  2. 0 README.markdown
  3. +20 −0 bin/lyndon
  4. +9 −0 johnson.rb
  5. +23 −0 lib/lyndon.rb
  6. +24 −0 lib/lyndon/coercion.rb
  7. +43 −0 lib/lyndon/repl.rb
  8. +14 −0 lib/lyndon/runtime.rb
20 LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2009 Chris Wanstrath
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
No changes.
@@ -0,0 +1,20 @@
+#!/usr/bin/env macruby
+
+__DIR__ = File.expand_path(File.dirname(__FILE__))
+$LOAD_PATH.unshift File.join(__DIR__, '..', 'lib')
+
+require 'lyndon'
+require 'lyndon/repl'
+
+case ARGV.first
+when '-h', '--help'
+ puts "Run it:" ," $ lyndon -e '(string to eval)'", " $ lyndon -p '(string to eval; result will puts)'", " $ lyndon file.lyndon", " $ lyndon"
+when '-e'
+ Lyndon.eval(ARGV.last)
+when '-p'
+ puts Lyndon.eval(ARGV.last)
+when nil
+ Lyndon::Repl.start
+else
+ Lyndon.eval_file(ARGV.last)
+end
@@ -0,0 +1,9 @@
+# http://developer.apple.com/documentation/Cocoa/Reference/WebKit/ObjC_classic/index.html
+
+framework 'WebKit'
+
+webView = WebView.new
+object = webView.windowScriptObject
+
+object.evaluateWebScript("var name = 'chris'")
+puts object.evaluateWebScript("name")
@@ -0,0 +1,23 @@
+#
+# It's pretty much that easy.
+#
+
+if !defined?(RUBY_ENGINE) || RUBY_ENGINE != 'macruby'
+ abort "Lyndon requires MacRuby. http://www.macruby.org/"
+end
+
+framework 'WebKit'
+
+require 'lyndon/runtime'
+require 'lyndon/coercion'
+
+module Lyndon
+ def self.eval(js)
+ Runtime.new.eval(js)
+ end
+
+ def self.eval_file(file)
+ contents = File.read(File.expand_path(file))
+ eval(contents)
+ end
+end
@@ -0,0 +1,24 @@
+# help convert objects WebKit's JS returns into
+# things we can use
+
+class NSCFNumber
+ def inspect
+ if Integer(self) == Float(self)
+ Integer(self).to_s
+ else
+ Float(self).to_s
+ end
+ end
+end
+
+class NSCFBoolean
+ def inspect
+ (self == NSNumber.numberWithBool(true)).to_s
+ end
+end
+
+class WebScriptObject
+ def inspect
+ callWebScriptMethod("toString", withArguments:nil)
+ end
+end
@@ -0,0 +1,43 @@
+require 'readline'
+
+module Lyndon
+ class Repl
+ Prompt = '>> '
+ AwaitingInput = '?> '
+ Result = '=> '
+ HistoryFile = File.join(File.expand_path('~'), '.lyndon_history')
+
+ def self.start
+ load_history
+ @parser = Lyndon::Runtime.new
+
+ loop do
+ input = Readline.readline(Prompt)
+ quit if input.nil?
+
+ begin
+ puts Result + @parser.eval(input).inspect
+ rescue => e
+ puts "Error: #{e}", e.backtrace
+ quit
+ else
+ Readline::HISTORY.push(input)
+ end
+ end
+ end
+
+ def self.load_history
+ return unless File.exists? HistoryFile
+ File.readlines(HistoryFile).each do |line|
+ Readline::HISTORY.push line.chomp
+ end
+ end
+
+ def self.quit
+ File.open(HistoryFile, 'w') do |f|
+ f.puts Readline::HISTORY.to_a.join("\n")
+ end
+ exit
+ end
+ end
+end
@@ -0,0 +1,14 @@
+# http://developer.apple.com/documentation/Cocoa/Reference/WebKit/ObjC_classic/index.html
+
+module Lyndon
+ class Runtime
+ def initialize
+ @webView = WebView.new
+ @scripter = @webView.windowScriptObject
+ end
+
+ def eval(js)
+ @scripter.evaluateWebScript(js)
+ end
+ end
+end

0 comments on commit e889787

Please sign in to comment.