From 77ae5437842b8a23e466f90e09cdb91b745267dc Mon Sep 17 00:00:00 2001 From: James Coglan Date: Tue, 28 May 2019 16:25:34 +0100 Subject: [PATCH] Reformat the C and Java native extension modules. --- .../WebsocketMaskService.java | 68 ++++++++++--------- ext/websocket-driver/websocket_mask.c | 57 +++++++--------- websocket-driver.gemspec | 2 +- 3 files changed, 60 insertions(+), 67 deletions(-) diff --git a/ext/websocket-driver/WebsocketMaskService.java b/ext/websocket-driver/WebsocketMaskService.java index 20eb6fe..47eb2c6 100644 --- a/ext/websocket-driver/WebsocketMaskService.java +++ b/ext/websocket-driver/WebsocketMaskService.java @@ -1,8 +1,6 @@ package com.jcoglan.websocket; -import java.lang.Long; import java.io.IOException; - import org.jruby.Ruby; import org.jruby.RubyClass; import org.jruby.RubyModule; @@ -15,41 +13,45 @@ import org.jruby.runtime.builtin.IRubyObject; import org.jruby.runtime.load.BasicLibraryService; public class WebsocketMaskService implements BasicLibraryService { - private Ruby runtime; + private Ruby runtime; - public boolean basicLoad(Ruby runtime) throws IOException { - this.runtime = runtime; - RubyModule websocket = runtime.defineModule("WebSocket"); + public boolean basicLoad(Ruby runtime) throws IOException { + this.runtime = runtime; - RubyClass webSocketMask = websocket.defineClassUnder("Mask", runtime.getObject(), new ObjectAllocator() { - public IRubyObject allocate(Ruby runtime, RubyClass rubyClass) { - return new WebsocketMask(runtime, rubyClass); - } - }); + RubyModule websocket = runtime.defineModule("WebSocket"); + RubyClass webSocketMask = websocket.defineClassUnder("Mask", runtime.getObject(), getAllocator()); - webSocketMask.defineAnnotatedMethods(WebsocketMask.class); - return true; - } - - public class WebsocketMask extends RubyObject { - public WebsocketMask(final Ruby runtime, RubyClass rubyClass) { - super(runtime, rubyClass); + webSocketMask.defineAnnotatedMethods(WebsocketMask.class); + return true; } - @JRubyMethod - public IRubyObject mask(ThreadContext context, IRubyObject payload, IRubyObject mask) { - if (mask.isNil()) return payload; - - byte[] payload_a = ((RubyString)payload).getBytes(); - byte[] mask_a = ((RubyString)mask).getBytes(); - int i, n = payload_a.length; - - if (n == 0) return payload; - - for (i = 0; i < n; i++) { - payload_a[i] ^= mask_a[i % 4]; - } - return RubyString.newStringNoCopy(runtime, payload_a); + ObjectAllocator getAllocator() { + return new ObjectAllocator() { + public IRubyObject allocate(Ruby runtime, RubyClass rubyClass) { + return new WebsocketMask(runtime, rubyClass); + } + }; + } + + public class WebsocketMask extends RubyObject { + public WebsocketMask(final Ruby runtime, RubyClass rubyClass) { + super(runtime, rubyClass); + } + + @JRubyMethod + public IRubyObject mask(ThreadContext context, IRubyObject payload, IRubyObject mask) { + if (mask.isNil()) return payload; + + byte[] payload_a = ((RubyString)payload).getBytes(); + byte[] mask_a = ((RubyString)mask).getBytes(); + int i, n = payload_a.length; + + if (n == 0) return payload; + + for (i = 0; i < n; i++) { + payload_a[i] ^= mask_a[i % 4]; + } + return RubyString.newStringNoCopy(runtime, payload_a); + } } - } } diff --git a/ext/websocket-driver/websocket_mask.c b/ext/websocket-driver/websocket_mask.c index 064d8e5..e7058b5 100644 --- a/ext/websocket-driver/websocket_mask.c +++ b/ext/websocket-driver/websocket_mask.c @@ -1,41 +1,32 @@ #include -VALUE WebSocket = Qnil; -VALUE WebSocketMask = Qnil; - -void Init_websocket_mask(); -VALUE method_websocket_mask(VALUE self, VALUE payload, VALUE mask); - -void -Init_websocket_mask() +VALUE method_websocket_mask(VALUE self, VALUE payload, VALUE mask) { - WebSocket = rb_define_module("WebSocket"); - WebSocketMask = rb_define_module_under(WebSocket, "Mask"); - rb_define_singleton_method(WebSocketMask, "mask", method_websocket_mask, 2); + char *payload_s, *mask_s, *unmasked_s; + long i, n; + VALUE unmasked; + + if (mask == Qnil || RSTRING_LEN(mask) != 4) { + return payload; + } + + payload_s = RSTRING_PTR(payload); + mask_s = RSTRING_PTR(mask); + n = RSTRING_LEN(payload); + + unmasked = rb_str_new(0, n); + unmasked_s = RSTRING_PTR(unmasked); + + for (i = 0; i < n; i++) { + unmasked_s[i] = payload_s[i] ^ mask_s[i % 4]; + } + return unmasked; } -VALUE -method_websocket_mask(VALUE self, - VALUE payload, - VALUE mask) +void Init_websocket_mask() { - char *payload_s, *mask_s, *unmasked_s; - long i, n; - VALUE unmasked; + VALUE WebSocket = rb_define_module("WebSocket"); + VALUE Mask = rb_define_module_under(WebSocket, "Mask"); - if (mask == Qnil || RSTRING_LEN(mask) != 4) { - return payload; - } - - payload_s = RSTRING_PTR(payload); - mask_s = RSTRING_PTR(mask); - n = RSTRING_LEN(payload); - - unmasked = rb_str_new(0, n); - unmasked_s = RSTRING_PTR(unmasked); - - for (i = 0; i < n; i++) { - unmasked_s[i] = payload_s[i] ^ mask_s[i % 4]; - } - return unmasked; + rb_define_singleton_method(Mask, "mask", method_websocket_mask, 2); } diff --git a/websocket-driver.gemspec b/websocket-driver.gemspec index a9fae5a..b5e698b 100644 --- a/websocket-driver.gemspec +++ b/websocket-driver.gemspec @@ -28,6 +28,6 @@ Gem::Specification.new do |s| s.add_development_dependency 'eventmachine' s.add_development_dependency 'permessage_deflate' - s.add_development_dependency 'rake-compiler', '~> 0.8.0' + s.add_development_dependency 'rake-compiler' s.add_development_dependency 'rspec' end