diff --git a/Rakefile b/Rakefile index 17c06073..55354535 100644 --- a/Rakefile +++ b/Rakefile @@ -238,7 +238,11 @@ if defined?(RUBY_ENGINE) and RUBY_ENGINE == 'jruby' task :release => :build else desc "Compiling extension" - task :compile => [ EXT_PARSER_DL, EXT_GENERATOR_DL ] + if RUBY_ENGINE == 'truffleruby' + task :compile => [ EXT_PARSER_DL ] + else + task :compile => [ EXT_PARSER_DL, EXT_GENERATOR_DL ] + end file EXT_PARSER_DL => EXT_PARSER_SRC do cd EXT_PARSER_DIR do diff --git a/ext/json/ext/generator/extconf.rb b/ext/json/ext/generator/extconf.rb index 8627c5f4..cf8d5f2b 100644 --- a/ext/json/ext/generator/extconf.rb +++ b/ext/json/ext/generator/extconf.rb @@ -1,4 +1,9 @@ require 'mkmf' -$defs << "-DJSON_GENERATOR" -create_makefile 'json/ext/generator' +if RUBY_ENGINE == 'truffleruby' + # The pure-Ruby generator is faster on TruffleRuby, so skip compiling the generator extension + File.write('Makefile', dummy_makefile("").join) +else + $defs << "-DJSON_GENERATOR" + create_makefile 'json/ext/generator' +end diff --git a/lib/json/ext.rb b/lib/json/ext.rb index 7264a857..b62e2317 100644 --- a/lib/json/ext.rb +++ b/lib/json/ext.rb @@ -4,11 +4,19 @@ module JSON # This module holds all the modules/classes that implement JSON's # functionality as C extensions. module Ext - require 'json/ext/parser' - require 'json/ext/generator' - $DEBUG and warn "Using Ext extension for JSON." - JSON.parser = Parser - JSON.generator = Generator + if RUBY_ENGINE == 'truffleruby' + require 'json/ext/parser' + require 'json/pure' + $DEBUG and warn "Using Ext extension for JSON parser and Pure library for JSON generator." + JSON.parser = Parser + JSON.generator = JSON::Pure::Generator + else + require 'json/ext/parser' + require 'json/ext/generator' + $DEBUG and warn "Using Ext extension for JSON." + JSON.parser = Parser + JSON.generator = Generator + end end JSON_LOADED = true unless defined?(::JSON::JSON_LOADED)