From 0b55a51a916c873aa00d87a81138b74109c33511 Mon Sep 17 00:00:00 2001 From: kares Date: Thu, 2 Aug 2012 13:11:22 +0200 Subject: [PATCH] add Rhino.implementation_version for checking loaded jar rhino 1.7R4 compatibility with prototype exposing (in specs) --- lib/rhino.rb | 17 +++++++++++ spec/rhino/rhino_ext_spec.rb | 58 ++++++++++++++++++++++++------------ 2 files changed, 56 insertions(+), 19 deletions(-) diff --git a/lib/rhino.rb b/lib/rhino.rb index 38bf069..a860fd6 100644 --- a/lib/rhino.rb +++ b/lib/rhino.rb @@ -16,6 +16,23 @@ module Rhino end end + @@implementation_version = nil + # Helper to resolve what version of Rhino's .jar we're really using. + def self.implementation_version + @@implementation_version ||= begin + urls = JS::Kit.java_class.to_java.getClassLoader. + getResources('META-INF/MANIFEST.MF').to_a + rhino_jar_urls = urls.select { |url| url.toString.index(JAR_PATH) } + if rhino_jar_urls.empty? + raise "could not find #{JAR_PATH} manifest among: #{urls.map(&:toString).join(', ')}" + elsif rhino_jar_urls.size > 1 + raise "could not find #{JAR_PATH} manifest among: #{urls.map(&:toString).join(', ')}" + end + manifest = java.util.jar.Manifest.new rhino_jar_urls.first.openStream + manifest.getMainAttributes.getValue 'Implementation-Version' + end + end + end require 'rhino/version' diff --git a/spec/rhino/rhino_ext_spec.rb b/spec/rhino/rhino_ext_spec.rb index a6c9a13..10935c7 100644 --- a/spec/rhino/rhino_ext_spec.rb +++ b/spec/rhino/rhino_ext_spec.rb @@ -1,5 +1,19 @@ require File.expand_path('../spec_helper', File.dirname(__FILE__)) +module RhinoHelpers + + module_function + + def add_prototype_key(hash, recurse = false) + hash['prototype'] ||= {} + hash.keys.each do |key| + val = hash[key] unless key == 'prototype' + add_prototype_key(val, recurse) if val.is_a?(Hash) + end if recurse + end + +end + shared_examples_for 'ScriptableObject', :shared => true do it "acts like a hash" do @@ -40,7 +54,7 @@ describe "NativeObject" do end describe "FunctionObject" do - + before do factory = Rhino::JS::ContextFactory.new context, scope = nil, nil @@ -54,7 +68,9 @@ describe "FunctionObject" do @object = Rhino::JS::FunctionObject.new('to_string', to_string, scope) @object.instance_eval do def to_h_properties - { "arguments"=> nil, "prototype"=> {}, "name"=> "to_string", "arity"=> 0, "length"=> 0 } + h = { "arguments"=> nil, "name"=> "to_string", "arity"=> 0, "length"=> 0 } + RhinoHelpers.add_prototype_key(h) if Rhino.implementation_version < '1.7R4' + h end end end @@ -115,7 +131,7 @@ describe "NativeObject (scoped)" do end describe "NativeFunction" do - + before do factory = Rhino::JS::ContextFactory.new @context, @scope = nil, nil @@ -129,7 +145,9 @@ describe "NativeFunction" do @object = Rhino::JS::ScriptableObject.getProperty(object, 'toString') @object.instance_eval do def to_h_properties - { "arguments"=> nil, "prototype"=> {}, "name"=> "toString", "arity"=> 0, "length"=> 0 } + h = { "arguments"=> nil, "name"=> "toString", "arity"=> 0, "length"=> 0 } + RhinoHelpers.add_prototype_key(h) if Rhino.implementation_version < '1.7R4' + h end end end @@ -187,7 +205,7 @@ describe "NativeFunction" do end describe "NativeFunction (constructor)" do - + before do factory = Rhino::JS::ContextFactory.new context, scope = nil, nil @@ -200,23 +218,25 @@ describe "NativeFunction (constructor)" do @object = Rhino::JS::ScriptableObject.getProperty(context.newObject(scope), 'constructor') @object.instance_eval do def to_h_properties - { + h = { "arguments"=>nil, "prototype"=>{}, "name"=>"Object", "arity"=>1, "length"=>1, - "getPrototypeOf"=> { "arguments"=>nil, "prototype"=>{}, "name"=>"getPrototypeOf", "arity"=>1, "length"=>1}, - "keys"=>{"arguments"=>nil, "prototype"=>{}, "name"=>"keys", "arity"=>1, "length"=>1}, - "getOwnPropertyNames"=>{"arguments"=>nil, "prototype"=>{}, "name"=>"getOwnPropertyNames", "arity"=>1, "length"=>1}, - "getOwnPropertyDescriptor"=>{"arguments"=>nil, "prototype"=>{}, "name"=>"getOwnPropertyDescriptor", "arity"=>2, "length"=>2}, - "defineProperty"=>{"arguments"=>nil, "prototype"=>{}, "name"=>"defineProperty", "arity"=>3, "length"=>3}, - "isExtensible"=>{"arguments"=>nil, "prototype"=>{}, "name"=>"isExtensible", "arity"=>1, "length"=>1}, - "preventExtensions"=>{"arguments"=>nil, "prototype"=>{}, "name"=>"preventExtensions", "arity"=>1, "length"=>1}, - "defineProperties"=>{"arguments"=>nil, "prototype"=>{}, "name"=>"defineProperties", "arity"=>2, "length"=>2}, - "create"=>{"arguments"=>nil, "prototype"=>{}, "name"=>"create", "arity"=>2, "length"=>2}, - "isSealed"=>{"arguments"=>nil, "prototype"=>{}, "name"=>"isSealed", "arity"=>1, "length"=>1}, - "isFrozen"=>{"arguments"=>nil, "prototype"=>{}, "name"=>"isFrozen", "arity"=>1, "length"=>1}, - "seal"=>{"arguments"=>nil, "prototype"=>{}, "name"=>"seal", "arity"=>1, "length"=>1}, - "freeze"=>{"arguments"=>nil, "prototype"=>{}, "name"=>"freeze", "arity"=>1, "length"=>1} + "getPrototypeOf"=> { "arguments"=>nil, "name"=>"getPrototypeOf", "arity"=>1, "length"=>1}, + "keys"=>{"arguments"=>nil, "name"=>"keys", "arity"=>1, "length"=>1}, + "getOwnPropertyNames"=>{"arguments"=>nil, "name"=>"getOwnPropertyNames", "arity"=>1, "length"=>1}, + "getOwnPropertyDescriptor"=>{"arguments"=>nil, "name"=>"getOwnPropertyDescriptor", "arity"=>2, "length"=>2}, + "defineProperty"=>{"arguments"=>nil, "name"=>"defineProperty", "arity"=>3, "length"=>3}, + "isExtensible"=>{"arguments"=>nil, "name"=>"isExtensible", "arity"=>1, "length"=>1}, + "preventExtensions"=>{"arguments"=>nil, "name"=>"preventExtensions", "arity"=>1, "length"=>1}, + "defineProperties"=>{"arguments"=>nil, "name"=>"defineProperties", "arity"=>2, "length"=>2}, + "create"=>{"arguments"=>nil, "name"=>"create", "arity"=>2, "length"=>2}, + "isSealed"=>{"arguments"=>nil, "name"=>"isSealed", "arity"=>1, "length"=>1}, + "isFrozen"=>{"arguments"=>nil, "name"=>"isFrozen", "arity"=>1, "length"=>1}, + "seal"=>{"arguments"=>nil, "name"=>"seal", "arity"=>1, "length"=>1}, + "freeze"=>{"arguments"=>nil, "name"=>"freeze", "arity"=>1, "length"=>1} } + RhinoHelpers.add_prototype_key(h, :recurse) if Rhino.implementation_version < '1.7R4' + h end end end