smparkes-envjs 0.0.8 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/bin/envjsrb CHANGED
@@ -3,6 +3,8 @@
3
3
  require 'rubygems'
4
4
  require 'pathname'
5
5
 
6
+ require 'envjs/options'
7
+
6
8
  require 'johnson/tracemonkey'
7
9
  require 'johnson/cli'
8
10
  require 'envjs/runtime'
data/lib/envjs/env.js CHANGED
@@ -364,7 +364,7 @@ var print_exception = window.print_exception =
364
364
  lambda { |e| \
365
365
  estr = e.to_s; \
366
366
  estr.gsub!(/(<br \\/>)+/, %( )); \
367
- debug(%(Exception: ),estr,%(\n)); \
367
+ $stderr.print(%(Exception: ),estr,%(\n)); \
368
368
  begin; \
369
369
  e.stack.to_s.split(%(\n)).each do |line| \
370
370
  m = line.match(/(.*)@([^@]*)$/); \
@@ -375,7 +375,7 @@ lambda { |e| \
375
375
  if ( s.length > limit ); \
376
376
  s = s[0,limit] + %(...); \
377
377
  end; \
378
- debug(m[2],%( ),s,%(\n)); \
378
+ $stderr.print(m[2],%( ),s,%(\n)); \
379
379
  end; \
380
380
  rescue; end; \
381
381
  } \
@@ -396,7 +396,7 @@ lambda { |e| \
396
396
  if ( s.length > limit ); \
397
397
  s = s[0,limit] + %(...); \
398
398
  end; \
399
- debug(m[2],%( ),s,%(\n)); \
399
+ $stderr.print(m[2],%( ),s,%(\n)); \
400
400
  end; \
401
401
  rescue; end; \
402
402
  } \
@@ -686,15 +686,12 @@ var Envjs = function(){
686
686
  var types, type, src, i, base,
687
687
  docWrites = [],
688
688
  write = document.write,
689
- writeln = document.writeln;
690
- //temporarily replace document write becuase the function
691
- //has a different meaning during parsing
692
- /*document.write = function(text){
693
- docWrites.push(text);
694
- };*/
689
+ writeln = document.writeln,
690
+ okay = true;
691
+ var script_type = script.type === null ? "text/javascript" : script.type;
695
692
  try{
696
- if(script.type){
697
- types = script.type?script.type.split(";"):[];
693
+ if(script_type){
694
+ types = script_type?script_type.split(";"):[];
698
695
  for(i=0;i<types.length;i++){
699
696
  if($env.scriptTypes[types[i]]){
700
697
  if(script.src){
@@ -712,9 +709,7 @@ var Envjs = function(){
712
709
  try {
713
710
  load($env.location(script.src.match(/([^\?#]*)/)[1], base ));
714
711
  } catch(e) {
715
- $env.error("could not load script: " +
716
- $env.location(script.src.match(/([^\?#]*)/)[1], base ) +
717
- ": " + e + " thrown" );
712
+ okay = false;
718
713
  }
719
714
  //lets you register a function to execute
720
715
  //after the script is loaded
@@ -729,18 +724,23 @@ var Envjs = function(){
729
724
  $env.loadInlineScript(script);
730
725
  }
731
726
  }else{
732
- if(!script.src && script.type == "text/javascript"){
727
+ if(!script.src && script_type == "text/javascript"){
733
728
  $env.loadInlineScript(script);
729
+ } else {
730
+ // load prohbited ...
731
+ okay = false;
734
732
  }
735
733
  }
736
734
  }
737
735
  }else{
736
+ // SMP this branch is probably dead ...
738
737
  //anonymous type and anonymous src means inline
739
738
  if(!script.src){
740
739
  $env.loadInlineScript(script);
741
740
  }
742
741
  }
743
742
  }catch(e){
743
+ okay = false;
744
744
  $env.error("Error loading script.", e);
745
745
  $env.onScriptLoadError(script);
746
746
  }finally{
@@ -751,6 +751,7 @@ var Envjs = function(){
751
751
  document.write = write;
752
752
  document.writeln = writeln;*/
753
753
  }
754
+ return okay;
754
755
  };
755
756
 
756
757
  $env.loadInlineScript = $env.loadInlineScript || function(script){};
@@ -4453,61 +4454,6 @@ var DOMImplementation = function() {
4453
4454
  this.errorChecking = true; // by default, test for exceptions
4454
4455
  };
4455
4456
 
4456
- /*
4457
- var __endHTMLElement__ = function(node, doc, p){
4458
- if(node.nodeName.toLowerCase() == 'script'){
4459
- // unless we're parsing in a window context, don't execute scripts
4460
- if (doc.parentWindow){
4461
- p.replaceEntities = true;
4462
- $env.loadLocalScript(node, p);
4463
-
4464
- // only fire event if we actually had something to load
4465
- if (node.src && node.src.length > 0){
4466
- var event = doc.createEvent();
4467
- event.initEvent("load");
4468
- node.dispatchEvent( event, false );
4469
- }
4470
- }
4471
- }
4472
- else if (node.nodeName.toLowerCase() == 'frame' ||
4473
- node.nodeName.toLowerCase() == 'iframe' ){
4474
-
4475
- if (node.src && node.src.length > 0){
4476
- $debug("getting content document for (i)frame from " + node.src);
4477
-
4478
- // any JS here is DOM-instigated, so the JS scope is the window, not the first script
4479
-
4480
- var save = $master.first_script_window;
4481
- $master.first_script_window = window;
4482
-
4483
- $env.loadFrame(node, $env.location(node.src));
4484
-
4485
- $master.first_script_window = save;
4486
-
4487
- var event = doc.createEvent();
4488
- event.initEvent("load");
4489
- node.dispatchEvent( event, false );
4490
- }
4491
- }
4492
- else if (node.nodeName.toLowerCase() == 'link'){
4493
- if (node.href && node.href.length > 0){
4494
- // don't actually load anything, so we're "done" immediately:
4495
- var event = doc.createEvent();
4496
- event.initEvent("load");
4497
- node.dispatchEvent( event, false );
4498
- }
4499
- }
4500
- else if (node.nodeName.toLowerCase() == 'img'){
4501
- if (node.src && node.src.length > 0){
4502
- // don't actually load anything, so we're "done" immediately:
4503
- var event = doc.createEvent();
4504
- event.initEvent("load");
4505
- node.dispatchEvent( event, false );
4506
- }
4507
- }
4508
- }
4509
- */
4510
-
4511
4457
  __extend__(DOMImplementation.prototype,{
4512
4458
  // @param feature : string - The package name of the feature to test.
4513
4459
  // the legal only values are "XML" and "CORE" (case-insensitive).
@@ -6080,22 +6026,22 @@ __extend__(HTMLDocument.prototype, {
6080
6026
  });
6081
6027
 
6082
6028
  var __elementPopped__ = function(ns, name, node){
6083
- //print('Element Popped: '+ns+" "+name+ " " );
6029
+ // print('Element Popped: '+ns+" "+name+ " "+ node+" " +node.type+" "+node.nodeName);
6084
6030
  var doc = __ownerDocument__(node);
6031
+ var type = ( node.type === null ) ? "text/javascript" : node.type;
6085
6032
  try{
6086
- if(node.nodeName.toLowerCase() == 'script' && node.type !== undefined){
6033
+ if(node.nodeName.toLowerCase() == 'script' && type == "text/javascript"){
6087
6034
  //$env.debug("element popped: script\n"+node.xml);
6088
6035
  // unless we're parsing in a window context, don't execute scripts
6089
6036
  if (doc.parentWindow){
6090
6037
  //p.replaceEntities = true;
6091
- $env.loadLocalScript(node, null);
6092
-
6038
+ var okay = $env.loadLocalScript(node, null);
6093
6039
  // only fire event if we actually had something to load
6094
6040
  if (node.src && node.src.length > 0){
6095
6041
  var event = doc.createEvent();
6096
- event.initEvent("load");
6042
+ event.initEvent( okay ? "load" : "error" );
6097
6043
  node.dispatchEvent( event, false );
6098
- }
6044
+ }
6099
6045
  }
6100
6046
  }
6101
6047
  else if (node.nodeName.toLowerCase() == 'frame' ||
@@ -8051,6 +7997,9 @@ __extend__(HTMLScriptElement.prototype, {
8051
7997
  },
8052
7998
  onload: function(event){
8053
7999
  __eval__(this.getAttribute('onload')||'', this);
8000
+ },
8001
+ onerror: function(event){
8002
+ __eval__(this.getAttribute('onerror')||'', this);
8054
8003
  }
8055
8004
  });
8056
8005
 
@@ -4,6 +4,10 @@ require 'envjs/net'
4
4
 
5
5
  class Envjs::Net::File < Net::Protocol
6
6
 
7
+ class << self
8
+ attr_accessor :on_open
9
+ end
10
+
7
11
  class Get
8
12
  attr_accessor :path
9
13
  def initialize path
@@ -28,6 +32,8 @@ class Envjs::Net::File < Net::Protocol
28
32
  class Response
29
33
  def initialize path
30
34
  @path = path
35
+ on_open = Envjs::Net::File.on_open
36
+ on_open and on_open.call( path )
31
37
  @file = File.new @path
32
38
  @body = @file.read
33
39
  @code = @file.nil? ? "404" : "200";
@@ -0,0 +1,11 @@
1
+ require 'optparse'
2
+
3
+ $envjsrb_deps = nil
4
+
5
+ OptionParser.new do |o|
6
+
7
+ o.on("--deps path") do |path|
8
+ $envjsrb_deps = path
9
+ end
10
+
11
+ end.parse!
data/lib/envjs/runtime.rb CHANGED
@@ -1,5 +1,8 @@
1
1
  require 'envjs'
2
2
  require "open-uri"
3
+ require 'rubygems'
4
+ require 'fsdb'
5
+ require 'envjs/net/file'
3
6
 
4
7
  module Envjs::Runtime
5
8
 
@@ -24,6 +27,7 @@ print = function() {
24
27
  }
25
28
  }
26
29
  Ruby.print("\n");
30
+ Ruby['$stdout'].flush();
27
31
  };
28
32
  EOJS
29
33
 
@@ -77,11 +81,69 @@ EOJS
77
81
  # calling this from JS is hosed; the ruby side is confused, maybe because HTTPHeaders is mixed in?
78
82
  master.add_req_field = lambda { |r,k,v| r.add_field(k,v) }
79
83
 
84
+ db = lambda do
85
+ $envjsrb_deps && ( @db ||= FSDB::Database.new $envjsrb_deps )
86
+ end
87
+
88
+ clear_deps = lambda do |w|
89
+ begin
90
+ if db.call
91
+ loc = w
92
+ begin loc = w.location; rescue; end
93
+ loc && ( loc = loc.to_s )
94
+ if ( loc !~ %r((http?s|file|about):) )
95
+ loc = "file://" + Pathname(loc).realpath.to_s
96
+ end
97
+ # $stderr.puts "clear", loc
98
+ if loc and loc != "about:blank"
99
+ paths = db.call[loc+".on.yml"] || []
100
+ paths.each do |path|
101
+ # $stderr.print "#{path} not by #{loc}\n";
102
+ db.call[path+".by.yml"].delete loc
103
+ end
104
+ # $stderr.print "#{loc} not on anything\n";
105
+ db.call.delete loc+".on.yml"
106
+ end
107
+ end
108
+ rescue Exception => e; $stderr.puts e; end
109
+ end
110
+
111
+ if $envjsrb_deps
112
+ Envjs::Net::File.on_open = clear_deps
113
+ end
114
+
115
+ add_dep = lambda do |w, f|
116
+ if db.call
117
+ loc = nil
118
+ begin loc = w.location; rescue; end
119
+ loc && ( loc = loc.to_s )
120
+ if ( loc && loc !~ %r((http?s|file|about):) )
121
+ loc = "file://" + Pathname(loc).realpath.to_s
122
+ end
123
+ path = f
124
+ if ( path !~ %r((http?s|file|about):) )
125
+ path = "file://" + Pathname(path).realpath.to_s
126
+ end
127
+ if loc and loc != "about:blank"
128
+ on = db.call[loc+".on.yml"] || []
129
+ on << path
130
+ db.call[loc+".on.yml"] = on
131
+ by = db.call[path+".by.yml"] = []
132
+ by << loc
133
+ db.call[path+".by.yml"] = by
134
+ # $stderr.print "#{loc} on #{path}: #{db.call[loc+'.on.yml']}\n"
135
+ # $stderr.print "#{path} by #{loc}: #{db.call[path+'.by.yml']}\n"
136
+ end
137
+ end
138
+ end
139
+
80
140
  master.load = lambda { |*files|
81
141
  if files.length == 2 && !(String === files[1])
82
142
  f = files[0]
83
143
  w = files[1]
84
144
  v = open(f).read.gsub(/\A#!.*$/, '')
145
+ loc = nil
146
+ add_dep.call w, f
85
147
  evaluate(v, f, 1, w, w, f)
86
148
  else
87
149
  load *files
@@ -0,0 +1,133 @@
1
+ require 'envjs'
2
+
3
+ module Envjs::Runtime
4
+
5
+ def self.extended object
6
+ object.instance_eval do
7
+
8
+ @outer = {} # ||= new_split_global_outer
9
+ @inner = {} # ||= new_split_global_inner( @outer )
10
+
11
+ @inner["Johnson"] = global["Johnson"]
12
+ @inner["Ruby"] = global["Ruby"]
13
+
14
+ evaluate <<'EOJS'
15
+ print = function() {
16
+ var l = arguments.length
17
+ for( var i = 0; i < l; i++ ) {
18
+ var s;
19
+ if ( arguments[i] === null ) {
20
+ s = "null";
21
+ } else if ( arguments[i] === undefined ) {
22
+ s = "undefined"
23
+ } else {
24
+ s = arguments[i].toString();
25
+ }
26
+ Ruby.print(s);
27
+ if( i < l-1 ) {
28
+ Ruby.print(" ");
29
+ }
30
+ }
31
+ Ruby.print("\n");
32
+ };
33
+ EOJS
34
+
35
+ @inner["print"] = global["print"]
36
+
37
+ ( class << self; self; end ).send :define_method, :evaluate do |*args|
38
+ ( script, file, line, global, scope ) = *args
39
+ g ||= @inner
40
+ s ||= @inner
41
+ p "super"
42
+ super( script, file, line, g, s )
43
+ end
44
+
45
+
46
+ # self.global = outer
47
+
48
+ return
49
+
50
+ evaluate <<'EOJS'
51
+ print = function() {
52
+ var l = arguments.length
53
+ for( var i = 0; i < l; i++ ) {
54
+ var s;
55
+ if ( arguments[i] === null ) {
56
+ s = "null";
57
+ } else if ( arguments[i] === undefined ) {
58
+ s = "undefined"
59
+ } else {
60
+ s = arguments[i].toString();
61
+ }
62
+ Ruby.print(s);
63
+ if( i < l-1 ) {
64
+ Ruby.print(" ");
65
+ }
66
+ }
67
+ Ruby.print("\n");
68
+ };
69
+ EOJS
70
+
71
+ master = global["$master"] = evaluate("new Object")
72
+ master.symbols = [ "Johnson", "Ruby", "print", "load", "whichInterpreter", "multiwindow" ]
73
+ # master.symbols = [ "Johnson", "whichInterpreter" ]
74
+ master.symbols.each { |symbol| master[symbol] = global[symbol] }
75
+
76
+ master.whichInterpreter = "Johnson"
77
+
78
+ if true
79
+
80
+ master.multiwindow = true
81
+
82
+ master.load = lambda { |*files| load *files }
83
+
84
+ master.evaluate = lambda { |script|
85
+ return evaluate(script, nil, nil, master["inner"], master["inner"])
86
+ }
87
+
88
+ master.global = lambda { global }
89
+
90
+ master.new_global = lambda { new_global }
91
+
92
+ master.set_global = lambda { |target, s| self.global = target }
93
+
94
+ master._new_proxy = lambda { |target| new_proxy target }
95
+ master._set_proxy_target = lambda { |proxy, target| set_proxy_target proxy, target }
96
+
97
+ master.new_split_global_outer = lambda { new_split_global_outer }
98
+ master.new_split_global_inner = lambda { |outer,_| new_split_global_inner outer }
99
+
100
+
101
+ end
102
+
103
+ # create an proto window object and proxy
104
+
105
+ outer = master["outer"] = new_split_global_outer
106
+ window = inner = master["inner"] = new_split_global_inner( outer )
107
+
108
+ master.symbols.each do |symbol|
109
+ window[symbol] = master[symbol]
110
+ end
111
+
112
+ master.first_script_window = window
113
+
114
+ window["$master"] = master
115
+
116
+ self.global = outer
117
+
118
+ ( class << self; self; end ).send :define_method, :wait do
119
+ master["finalize"] && master.finalize.call
120
+ end
121
+
122
+ ( class << self; self; end ).send :define_method, :evaluate do |*args|
123
+ ( script, file, line, global, scope ) = *args
124
+ global ||= inner
125
+ scope ||= inner
126
+ p "super"
127
+ super( script, file, line, global, scope )
128
+ end
129
+
130
+ end
131
+ end
132
+
133
+ end