binding_ninja 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 899b0528822b8f1c8d0a6fee76408380c2538135
4
- data.tar.gz: da1a9a09bbb88185545b48f3392afe3bc302e407
2
+ SHA256:
3
+ metadata.gz: 679a63081d35c6e533a8612822261637891b953ecb78d5bb2ce5a3dcd8aeeeb3
4
+ data.tar.gz: 1b3f41e8334d6a5baa7a681fe5c4645cad217f33ffd83b504e8138ce4076d46d
5
5
  SHA512:
6
- metadata.gz: 3a9387af40b0f409c4bd85588ebacb59bc82fcfe11a376bed8c1afff976a0a2898951d081b9eab6616071d23846f94c99f1db4490be3c37cfd31dec1f885dcdc
7
- data.tar.gz: 499ce2cfe87e0e913556c2bd3d5ac793ce8ca13325d4309dbbd2e9d7f73015b1736d6160f4c03dfceb6bd3c819529c8a79bcc047f48f33177c3331f96794862f
6
+ metadata.gz: 9f4881e0258a065023f9a84e3dda9c3bdd480ab4c6e261fc7db9d071075cacfee55172e380d5a9bef34b221f23ca8772629b8b876034d20819bb65072bc2cfe0
7
+ data.tar.gz: 5dcb4a7c8a27fe846cbeb5360eed826e769a2fed5058a06e2c4c72bdd390a7223a6aa5377d9ead6b998c120eafb88d04caea89f92a8f7774b36813cd2993cae3
data/.gitignore CHANGED
@@ -12,6 +12,8 @@
12
12
  *.o
13
13
  *.a
14
14
  mkmf.log
15
+ lib/binding_ninja/binding_ninja.jar
15
16
 
16
17
  # rspec failure tracking
17
18
  .rspec_status
19
+ .ruby-version
@@ -1,6 +1,8 @@
1
1
  sudo: false
2
2
  language: ruby
3
3
  rvm:
4
- - 2.3.2
5
- - 2.4.1
6
- before_install: gem install bundler -v 1.15.4
4
+ - 2.5.5
5
+ - 2.6.3
6
+ - jruby-9.2.6.0
7
+ - jruby-9.2.7.0
8
+ before_install: gem install bundler -v 2.0.1
data/Rakefile CHANGED
@@ -3,12 +3,21 @@ require "rspec/core/rake_task"
3
3
 
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
 
6
- require "rake/extensiontask"
7
-
8
6
  task :build => :compile
9
7
 
10
- Rake::ExtensionTask.new("binding_ninja") do |ext|
11
- ext.lib_dir = "lib/binding_ninja"
8
+ case RUBY_PLATFORM
9
+ when /java/
10
+ require 'rake/javaextensiontask'
11
+ Rake::JavaExtensionTask.new('binding_ninja') do |ext|
12
+ ext.lib_dir = "lib/binding_ninja"
13
+ ext.source_version = '1.8'
14
+ ext.target_version = '1.8'
15
+ end
16
+ else
17
+ require 'rake/extensiontask'
18
+ Rake::ExtensionTask.new("binding_ninja") do |ext|
19
+ ext.lib_dir = "lib/binding_ninja"
20
+ end
12
21
  end
13
22
 
14
23
  task :default => [:clobber, :compile, :spec]
@@ -20,9 +20,16 @@ Gem::Specification.new do |spec|
20
20
  spec.bindir = "exe"
21
21
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
22
  spec.require_paths = ["lib"]
23
- spec.extensions = ["ext/binding_ninja/extconf.rb"]
24
23
 
25
- spec.add_development_dependency "bundler", "~> 1.15"
24
+ case RUBY_PLATFORM
25
+ when /java/
26
+ spec.platform = "java"
27
+ spec.files << "lib/binding_ninja/binding_ninja.jar"
28
+ else
29
+ spec.extensions = ["ext/binding_ninja/extconf.rb"]
30
+ end
31
+
32
+ spec.add_development_dependency "bundler", ">= 1.15"
26
33
  spec.add_development_dependency "rake", "~> 10.0"
27
34
  spec.add_development_dependency "rake-compiler"
28
35
  spec.add_development_dependency "rspec", "~> 3.0"
@@ -0,0 +1,18 @@
1
+ package io.github.joker1007;
2
+
3
+ import java.io.IOException;
4
+
5
+ import org.jruby.Ruby;
6
+ import org.jruby.RubyHash;
7
+ import org.jruby.RubyModule;
8
+ import org.jruby.runtime.load.BasicLibraryService;
9
+
10
+ public class BindingNinjaService implements BasicLibraryService {
11
+ @Override
12
+ public boolean basicLoad(final Ruby runtime) throws IOException {
13
+ RubyModule bindingNinja = runtime.defineModule("BindingNinja");
14
+ bindingNinja.setInstanceVariable("@auto_inject_binding_extensions", RubyHash.newHash(runtime));
15
+ bindingNinja.defineAnnotatedMethods(RubyBindingNinja.class);
16
+ return true;
17
+ }
18
+ }
@@ -0,0 +1,154 @@
1
+ package io.github.joker1007;
2
+
3
+ import java.util.stream.Stream;
4
+ import org.jruby.anno.JRubyMethod;
5
+ import org.jruby.anno.JRubyModule;
6
+ import org.jruby.ast.util.ArgsUtil;
7
+ import org.jruby.internal.runtime.methods.JavaMethod;
8
+ import org.jruby.Ruby;
9
+ import org.jruby.RubyBasicObject;
10
+ import org.jruby.RubyBinding;
11
+ import org.jruby.RubyClass;
12
+ import org.jruby.RubyHash;
13
+ import org.jruby.RubyMethod;
14
+ import org.jruby.RubyModule;
15
+ import org.jruby.RubyProc;
16
+ import org.jruby.RubySymbol;
17
+ import org.jruby.runtime.Block;
18
+ import org.jruby.runtime.Constants;
19
+ import org.jruby.runtime.builtin.IRubyObject;
20
+ import org.jruby.runtime.Helpers;
21
+ import org.jruby.runtime.ThreadContext;
22
+ import org.jruby.runtime.Visibility;
23
+
24
+ @JRubyModule(name = "BindingNinja")
25
+ public class RubyBindingNinja {
26
+ static String OPTIONS_ID = "@auto_inject_binding_options";
27
+ static String EXTENSIONS_ID = "@auto_inject_binding_extensions";
28
+ static Integer[] jrubyVersionNums = Stream.of(Constants.VERSION.split("\\.")).map(Integer::parseInt).toArray(Integer[]::new);
29
+
30
+ @JRubyMethod(name = "auto_inject_binding", module = true, visibility = Visibility.PRIVATE, required = 1, optional = 1)
31
+ public static IRubyObject autoInjectBinding(ThreadContext context, IRubyObject recv, IRubyObject[] args) {
32
+ final Ruby runtime = context.getRuntime();
33
+
34
+ final IRubyObject extensions = runtime.getModule("BindingNinja").getInstanceVariable(EXTENSIONS_ID);
35
+ final IRubyObject methodSym = args[0];
36
+
37
+ IRubyObject ivar = ((RubyModule)recv).getInstanceVariable(OPTIONS_ID);
38
+ final IRubyObject options;
39
+ if (ivar.isTrue()) {
40
+ options = ivar;
41
+ } else {
42
+ options = RubyHash.newHash(runtime);
43
+ ((RubyModule)recv).setInstanceVariable(OPTIONS_ID, options);
44
+ }
45
+
46
+ IRubyObject extModTmp =
47
+ extensions instanceof RubyHash ?
48
+ ((RubyHash)extensions).op_aref(context, recv) :
49
+ context.nil;
50
+ if (extModTmp.isNil()) {
51
+ extModTmp = RubyModule.newModule(runtime);
52
+ ((RubyHash)extensions).op_aset(context, recv, extModTmp);
53
+ }
54
+ final RubyModule extMod = (RubyModule)extModTmp;
55
+ if (!((RubyModule)recv).hasModuleInHierarchy(extMod)) {
56
+ ((RubyModule)recv).prependModule(extMod);
57
+ }
58
+
59
+ IRubyObject cond = RubyBasicObject.UNDEF;
60
+ if (args.length == 2) {
61
+ final RubyHash kwArgs = args[1].convertToHash();
62
+ final IRubyObject[] rets = ArgsUtil.extractKeywordArgs(runtime.getCurrentContext(), kwArgs, "if");
63
+ cond = rets[0];
64
+ }
65
+
66
+ if (cond != RubyBasicObject.UNDEF) {
67
+ ((RubyHash)options).op_aset(context, methodSym, cond);
68
+ }
69
+
70
+ final String mid = methodSym.asJavaString();
71
+ if (cond == RubyBasicObject.UNDEF) {
72
+ extMod.addMethod(mid, autoInjectBindingInvokeWithoutCond(extMod, mid));
73
+ } else {
74
+ final RubyClass klass = cond.getMetaClass().getRealClass();
75
+ if (klass == runtime.getProc() || klass == runtime.getSymbol()) {
76
+ extMod.addMethod(mid, autoInjectBindingInvoke(extMod, mid));
77
+ } else if (cond.isTrue()) {
78
+ extMod.addMethod(mid, autoInjectBindingInvokeWithoutCond(extMod, mid));
79
+ } else {
80
+ extMod.addMethod(mid, autoInjectBindingInvokeStub(extMod, mid));
81
+ }
82
+ }
83
+
84
+ return methodSym;
85
+ }
86
+
87
+ private static JavaMethod autoInjectBindingInvoke(final RubyModule extMod, String name) {
88
+ return new JavaMethod(extMod, Visibility.PUBLIC, name) {
89
+ @Override
90
+ public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject[] args, Block block) {
91
+ final IRubyObject options = Helpers.invoke(context, self.getMetaClass(), "auto_inject_binding_options");
92
+ IRubyObject cond = ((org.jruby.RubyHash)options).op_aref(context, RubySymbol.newSymbol(context.getRuntime(), name));
93
+
94
+ final RubyClass klass = cond.getMetaClass().getRealClass();
95
+ if (klass == context.getRuntime().getProc()) {
96
+ if (Math.abs(((RubyProc)cond).arity().getIntValue()) > 0) {
97
+ cond = ((RubyProc)cond).call(context, new IRubyObject[]{self});
98
+ } else {
99
+ cond = ((RubyProc)cond).call(context, new IRubyObject[]{});
100
+ }
101
+ } else if (klass == context.getRuntime().getSymbol()) {
102
+ final IRubyObject method = ((RubyBasicObject)self).method(cond);
103
+ final IRubyObject proc = ((RubyMethod) method).to_proc(context);
104
+ cond = ((RubyProc)proc).call(context, new IRubyObject[]{});
105
+ }
106
+
107
+ final IRubyObject[] unshiftedArgs = new IRubyObject[args.length + 1];
108
+ if (cond.isTrue()) {
109
+ unshiftedArgs[0] = RubyBinding.newBinding(context.getRuntime(), context.currentBinding());
110
+ } else {
111
+ unshiftedArgs[0] = context.nil;
112
+ }
113
+ System.arraycopy(args, 0, unshiftedArgs, 1, args.length);
114
+ if (jrubyVersionNums[0] >= 9 && jrubyVersionNums[1] >= 2 && jrubyVersionNums[2] >= 7) {
115
+ return Helpers.invokeSuper(context, self, clazz, name, unshiftedArgs, block);
116
+ } else {
117
+ return Helpers.invokeSuper(context, self, extMod, name, unshiftedArgs, block);
118
+ }
119
+ }
120
+ };
121
+ }
122
+
123
+ private static JavaMethod autoInjectBindingInvokeStub(final RubyModule extMod, String name) {
124
+ return new JavaMethod(extMod, Visibility.PUBLIC, name) {
125
+ @Override
126
+ public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject[] args, Block block) {
127
+ final IRubyObject[] unshiftedArgs = new IRubyObject[args.length + 1];
128
+ unshiftedArgs[0] = context.nil;
129
+ System.arraycopy(args, 0, unshiftedArgs, 1, args.length);
130
+ if (jrubyVersionNums[0] >= 9 && jrubyVersionNums[1] >= 2 && jrubyVersionNums[2] >= 7) {
131
+ return Helpers.invokeSuper(context, self, clazz, name, unshiftedArgs, block);
132
+ } else {
133
+ return Helpers.invokeSuper(context, self, extMod, name, unshiftedArgs, block);
134
+ }
135
+ }
136
+ };
137
+ }
138
+
139
+ private static JavaMethod autoInjectBindingInvokeWithoutCond(final RubyModule extMod, String name) {
140
+ return new JavaMethod(extMod, Visibility.PUBLIC, name) {
141
+ @Override
142
+ public IRubyObject call(ThreadContext context, IRubyObject self, RubyModule clazz, String name, IRubyObject[] args, Block block) {
143
+ final IRubyObject[] unshiftedArgs = new IRubyObject[args.length + 1];
144
+ unshiftedArgs[0] = RubyBinding.newBinding(context.getRuntime(), context.currentBinding());
145
+ System.arraycopy(args, 0, unshiftedArgs, 1, args.length);
146
+ if (jrubyVersionNums[0] >= 9 && jrubyVersionNums[1] >= 2 && jrubyVersionNums[2] >= 7) {
147
+ return Helpers.invokeSuper(context, self, clazz, name, unshiftedArgs, block);
148
+ } else {
149
+ return Helpers.invokeSuper(context, self, extMod, name, unshiftedArgs, block);
150
+ }
151
+ }
152
+ };
153
+ }
154
+ }
@@ -1,5 +1,11 @@
1
1
  require "binding_ninja/version"
2
- require "binding_ninja/binding_ninja"
2
+
3
+ if /java/ =~ RUBY_PLATFORM
4
+ require_relative 'binding_ninja/binding_ninja.jar'
5
+ Java::IoGithubJoker1007::BindingNinjaService.new.basicLoad(JRuby.runtime)
6
+ else
7
+ require "binding_ninja/binding_ninja"
8
+ end
3
9
 
4
10
  module BindingNinja
5
11
  def auto_inject_binding_options
@@ -1,3 +1,3 @@
1
1
  module BindingNinja
2
- VERSION = "0.2.2"
2
+ VERSION = "0.2.3"
3
3
  end
metadata CHANGED
@@ -1,27 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: binding_ninja
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - joker1007
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-11-15 00:00:00.000000000 Z
11
+ date: 2019-04-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.15'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.15'
27
27
  - !ruby/object:Gem::Dependency
@@ -85,6 +85,8 @@ files:
85
85
  - bin/console
86
86
  - bin/setup
87
87
  - binding_ninja.gemspec
88
+ - ext/binding_ninja/BindingNinjaService.java
89
+ - ext/binding_ninja/RubyBindingNinja.java
88
90
  - ext/binding_ninja/binding_ninja.c
89
91
  - ext/binding_ninja/binding_ninja.h
90
92
  - ext/binding_ninja/extconf.rb
@@ -109,8 +111,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
109
111
  - !ruby/object:Gem::Version
110
112
  version: '0'
111
113
  requirements: []
112
- rubyforge_project:
113
- rubygems_version: 2.6.13
114
+ rubygems_version: 3.0.3
114
115
  signing_key:
115
116
  specification_version: 4
116
117
  summary: pass binding of method caller implicitly