From 66ca90cec495a545638d65ca307dee35b382d322 Mon Sep 17 00:00:00 2001 From: skeshari12 Date: Tue, 12 Apr 2022 14:35:15 +0530 Subject: add ruby 3.1 in verfiy pipeline Signed-off-by: skeshari12 --- .expeditor/buildkite/run_linux_tests.sh | 14 ++++++ .expeditor/buildkite/run_windows_tests.ps1 | 20 ++++++++ .expeditor/buildkite/verify.ps1 | 20 ++++---- .expeditor/verify.pipeline.yml | 73 ++++++++++++++++-------------- .github/CODEOWNERS | 4 +- 5 files changed, 84 insertions(+), 47 deletions(-) create mode 100755 .expeditor/buildkite/run_linux_tests.sh create mode 100644 .expeditor/buildkite/run_windows_tests.ps1 diff --git a/.expeditor/buildkite/run_linux_tests.sh b/.expeditor/buildkite/run_linux_tests.sh new file mode 100755 index 0000000..3774734 --- /dev/null +++ b/.expeditor/buildkite/run_linux_tests.sh @@ -0,0 +1,14 @@ +#!/bin/sh +# + +set -evx + +echo "---Bundle install---" +ruby --version +bundle --version +gem update --system +bundle install --without development_extras --jobs 3 --retry 3 --path vendor/bundle +gem install yajl-ruby json psych + +echo "---Bundle Exec---" +bundle exec rake \ No newline at end of file diff --git a/.expeditor/buildkite/run_windows_tests.ps1 b/.expeditor/buildkite/run_windows_tests.ps1 new file mode 100644 index 0000000..23f02c3 --- /dev/null +++ b/.expeditor/buildkite/run_windows_tests.ps1 @@ -0,0 +1,20 @@ +# Stop script execution when a non-terminating error occurs +$ErrorActionPreference = "Stop" + +# This will run ruby test on windows platform + +echo "--- Install make " +choco install make --source=cygwin + +Write-Output "--- Bundle install" +ruby --version +bundler --version +gem update --system +bundle install --without development_extras --jobs 3 --retry 3 --path vendor/bundle +gem install yajl-ruby json psych +If ($lastexitcode -ne 0) { Exit $lastexitcode } + +Write-Output "--- Bundle Execute" +bundle exec rake compile +bundle exec rake spec +If ($lastexitcode -ne 0) { Exit $lastexitcode } \ No newline at end of file diff --git a/.expeditor/buildkite/verify.ps1 b/.expeditor/buildkite/verify.ps1 index 106e1bf..c72188b 100644 --- a/.expeditor/buildkite/verify.ps1 +++ b/.expeditor/buildkite/verify.ps1 @@ -2,37 +2,37 @@ echo "--- system details" $Properties = 'Caption', 'CSName', 'Version', 'BuildType', 'OSArchitecture' Get-CimInstance Win32_OperatingSystem | Select-Object $Properties | Format-Table -AutoSize -echo "--- Install make and ruby2.devkit" -choco install make ruby ruby2.devkit -y +echo "--- Install make" +choco install make -y refreshenv - -echo - c:\tools\ruby26 > c:\tools\Devkit2\config.yml -ruby c:\tools\Devkit2\dk.rb install +If ($lastexitcode -ne 0) { Exit $lastexitcode } choco install msys2 -y refreshenv +If ($lastexitcode -ne 0) { Exit $lastexitcode } Write-Output 'Updating PATH' $env:PATH = "C:\tools\ruby26\bin;C:\tools\DevKit2\mingw\bin;C:\tools\DevKit2\bin;" + $env:PATH [Environment]::SetEnvironmentVariable('PATH', $env:PATH, [EnvironmentVariableTarget]::Machine) +If ($lastexitcode -ne 0) { Exit $lastexitcode } ruby -v bundle --version gem -v - -C:\tools\ruby26\ridk_use\ridk.cmd install 3 -C:\tools\ruby26\ridk_use\ridk.cmd enable +If ($lastexitcode -ne 0) { Exit $lastexitcode } echo "--- gem install bundler" gem install bundler +If ($lastexitcode -ne 0) { Exit $lastexitcode } echo "--- bundle install" bundle install --without development_extras --jobs 3 --retry 3 --path vendor/bundle +If ($lastexitcode -ne 0) { Exit $lastexitcode } echo "+++ bundle exec rake compile" bundle exec rake compile +If ($lastexitcode -ne 0) { Exit $lastexitcode } echo "+++ bundle exec rake spec" bundle exec rake spec - -exit $LASTEXITCODE \ No newline at end of file +If ($lastexitcode -ne 0) { Exit $lastexitcode } \ No newline at end of file diff --git a/.expeditor/verify.pipeline.yml b/.expeditor/verify.pipeline.yml index 06ca2f3..83ce300 100644 --- a/.expeditor/verify.pipeline.yml +++ b/.expeditor/verify.pipeline.yml @@ -3,12 +3,7 @@ steps: # Allow ruby-2.4 to softfail since it was configured so in travis too - label: ":ruby: 2.4" command: - - ruby --version - - bundle --version - - gem update --system - - bundle install --without development_extras --jobs 3 --retry 3 --path vendor/bundle - - gem install yajl-ruby json psych - - bundle exec rake + - .expeditor/buildkite/run_linux_tests.sh soft_fail: - exit_status: 1 expeditor: @@ -21,12 +16,7 @@ steps: - label: ":ruby: 2.5" command: - - ruby --version - - bundle --version - - gem update --system - - bundle install --without development_extras --jobs 3 --retry 3 --path vendor/bundle - - gem install yajl-ruby json psych - - bundle exec rake + - .expeditor/buildkite/run_linux_tests.sh expeditor: executor: docker: @@ -37,49 +27,62 @@ steps: - label: ":ruby: 2.6" command: - - ruby --version - - bundle --version - - gem update --system - - bundle install --without development_extras --jobs 3 --retry 3 --path vendor/bundle - - gem install yajl-ruby json psych - - bundle exec rake + - .expeditor/buildkite/run_linux_tests.sh expeditor: executor: docker: image: ruby:2.6 - privileged: true environment: - BUNDLE_GEMFILE=/workdir/Gemfile - FORCE_FFI_YAJL="ext" -# Allow jruby to softfail since it was configured so in travis too -- label: run-lint-and-specs-jruby +- label: ":ruby: 3.0" command: - - ruby --version - - bundle --version - - gem update --system - - apt-get update -y && apt-get install -y git make gcc - - bundle install --without development_extras --jobs 3 --retry 3 --path vendor/bundle - - gem install ffi json psych - - bundle exec rake - soft_fail: - - exit_status: 1 + - .expeditor/buildkite/run_linux_tests.sh expeditor: executor: docker: - image: jruby + image: ruby:3.0 + environment: + - BUNDLE_GEMFILE=/workdir/Gemfile + - FORCE_FFI_YAJL="ext" + +- label: ":ruby: 3.1" + command: + - .expeditor/buildkite/run_linux_tests.sh + expeditor: + executor: + docker: + image: ruby:3.1 privileged: true environment: - BUNDLE_GEMFILE=/workdir/Gemfile - - RUBY_PLATFORM="jruby" - - FORCE_FFI_YAJL="ffi" + - FORCE_FFI_YAJL="ext" + +- label: "run-specs-ruby-3.0-windows" + command: + - .expeditor/buildkite/run_windows_tests.ps1 + timeout_in_minutes: 20 + expeditor: + executor: + docker: + host_os: windows + shell: ["powershell", "-Command"] + image: rubydistros/windows-2019:3.0 + environment: + - BUNDLE_GEMFILE=/workdir/Gemfile + - FORCE_FFI_YAJL="ext" -- label: "run specs :windows:" +- label: "run-specs-ruby-3.1-windows" command: - - /workdir/.expeditor/buildkite/verify.ps1 + - .expeditor/buildkite/run_windows_tests.ps1 timeout_in_minutes: 20 expeditor: executor: docker: host_os: windows shell: ["powershell", "-Command"] + image: rubydistros/windows-2019:3.1 + environment: + - BUNDLE_GEMFILE=/workdir/Gemfile + - FORCE_FFI_YAJL="ext" diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 950afb7..0a26873 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -1,5 +1,5 @@ # Order is important. The last matching pattern has the most precedence. -* @chef/chef-foundation-owners @chef/chef-foundation-approvers @chef/chef-foundation-reviewers -.expeditor/ @chef/jex-team +* @chef/chef-infra-owners @chef/chef-infra-approvers @chef/chef-infra-reviewers +.expeditor/ @chef/infra-packages *.md @chef/docs-team -- cgit v1.2.1 From 0f05b88d19bf180499d4401c41918ff4a87b4d67 Mon Sep 17 00:00:00 2001 From: skeshari12 Date: Tue, 12 Apr 2022 15:50:38 +0530 Subject: fix error Signed-off-by: skeshari12 --- Gemfile | 14 +- ext/dlopen/dlopen.c | 40 +++++ ext/dlopen/extconf.rb | 16 ++ ext/encoder/encoder.c | 394 +++++++++++++++++++++++++++++++++++++++++++++++++ ext/encoder/extconf.rb | 65 ++++++++ ext/parser/extconf.rb | 65 ++++++++ ext/parser/parser.c | 240 ++++++++++++++++++++++++++++++ 7 files changed, 830 insertions(+), 4 deletions(-) create mode 100644 ext/dlopen/dlopen.c create mode 100644 ext/dlopen/extconf.rb create mode 100644 ext/encoder/encoder.c create mode 100644 ext/encoder/extconf.rb create mode 100644 ext/parser/extconf.rb create mode 100644 ext/parser/parser.c diff --git a/Gemfile b/Gemfile index 30fe201..6859635 100644 --- a/Gemfile +++ b/Gemfile @@ -6,13 +6,19 @@ group :development do # for testing loading concurrently with yajl-ruby, not on jruby # gem 'yajl-ruby', platforms: [ :ruby, :mswin, :mingw ] gem "ffi" - gem "rake", ">= 10.1" - gem "rspec", "~> 3.0" + gem "rake"#, ">= 10.1" + gem "rspec"#, "~> 3.0" gem "pry", "~> 0.9" - gem "rake-compiler", "~> 1.0" - gem "rack", "~> 2.0" + gem "rake-compiler"#, "~> 1.0" + gem "rack"#, "~> 2.0" end group :development_extras do gem "chefstyle" end + +instance_eval(ENV["GEMFILE_MOD"]) if ENV["GEMFILE_MOD"] + +# If you want to load debugging tools into the bundle exec sandbox, +# add these additional dependencies into Gemfile.local +eval_gemfile(__FILE__ + ".local") if File.exist?(__FILE__ + ".local") \ No newline at end of file diff --git a/ext/dlopen/dlopen.c b/ext/dlopen/dlopen.c new file mode 100644 index 0000000..eb81a21 --- /dev/null +++ b/ext/dlopen/dlopen.c @@ -0,0 +1,40 @@ +#include + +#if defined(HAVE_DLFCN_H) +# include +#ifndef RTLD_LAZY +#define RTLD_LAZY 0 +#endif +#ifndef RTLD_GLOBAL +#define RTLD_GLOBAL 0 +#endif +#ifndef RTLD_NOW +#define RTLD_NOW 0 +#endif +#else +# if defined(_WIN32) +# include +# define dlopen(name,flag) ((void*)LoadLibrary(name)) +# define dlerror() strerror(rb_w32_map_errno(GetLastError())) +# define dlsym(handle,name) ((void*)GetProcAddress((handle),(name))) +# define RTLD_LAZY -1 +# define RTLD_NOW -1 +# define RTLD_GLOBAL -1 +# endif +#endif + +static VALUE mFFI_Yajl, mDlopen, mExt; + +static VALUE mDlopen_dlopen(VALUE self, VALUE file) { + if (dlopen(RSTRING_PTR(file), RTLD_NOW|RTLD_GLOBAL) == NULL) { + rb_raise(rb_eArgError, "%s", dlerror()); + } + return Qnil; +} + +void Init_dlopen() { + mFFI_Yajl = rb_define_module("FFI_Yajl"); + mExt = rb_define_module_under(mFFI_Yajl, "Ext"); + mDlopen = rb_define_module_under(mExt, "Dlopen"); + rb_define_method(mDlopen, "dlopen", mDlopen_dlopen, 1); +} diff --git a/ext/dlopen/extconf.rb b/ext/dlopen/extconf.rb new file mode 100644 index 0000000..803d326 --- /dev/null +++ b/ext/dlopen/extconf.rb @@ -0,0 +1,16 @@ +# rubocop:disable Style/GlobalVars +require "mkmf" +require "rubygems" + +RbConfig::MAKEFILE_CONFIG["CC"] = ENV["CC"] if ENV["CC"] + +puts $CFLAGS +puts $LDFLAGS + +have_header("dlfcn.h") + +have_library("dl", "dlopen") + +dir_config "dlopen" + +create_makefile "ffi_yajl/ext/dlopen" diff --git a/ext/encoder/encoder.c b/ext/encoder/encoder.c new file mode 100644 index 0000000..6f1492c --- /dev/null +++ b/ext/encoder/encoder.c @@ -0,0 +1,394 @@ +#include +#include + +static VALUE mFFI_Yajl, mExt, mEncoder, mEncoder2, cEncodeError; +static VALUE cDate, cTime, cDateTime, cStringIO; +static VALUE cYajl_Gen; + +/* FIXME: the json gem does a whole bunch of indirection around monkeypatching... not sure if we need to as well... */ + +static VALUE mEncoder_do_yajl_encode(VALUE self, VALUE obj, VALUE yajl_gen_opts, VALUE json_opts) { + ID sym_ffi_yajl = rb_intern("ffi_yajl"); + VALUE sym_yajl_gen_beautify = ID2SYM(rb_intern("yajl_gen_beautify")); + VALUE sym_yajl_gen_validate_utf8 = ID2SYM(rb_intern("yajl_gen_validate_utf8")); + VALUE sym_yajl_gen_indent_string = ID2SYM(rb_intern("yajl_gen_indent_string")); + yajl_gen yajl_gen; + const unsigned char *buf; + size_t len; + VALUE state; + VALUE ret; + VALUE indent_string; + VALUE rb_yajl_gen; + + yajl_gen = yajl_gen_alloc(NULL); + + if ( rb_hash_aref(yajl_gen_opts, sym_yajl_gen_beautify) == Qtrue ) { + yajl_gen_config(yajl_gen, yajl_gen_beautify, 1); + } + if ( rb_hash_aref(yajl_gen_opts, sym_yajl_gen_validate_utf8) == Qtrue ) { + yajl_gen_config(yajl_gen, yajl_gen_validate_utf8, 1); + } + + indent_string = rb_hash_aref(yajl_gen_opts, sym_yajl_gen_indent_string); + if (indent_string != Qnil) { + yajl_gen_config(yajl_gen, yajl_gen_indent_string, RSTRING_PTR(indent_string)); + } else { + yajl_gen_config(yajl_gen, yajl_gen_indent_string, " "); + } + + state = rb_hash_new(); + + rb_hash_aset(state, rb_str_new2("processing_key"), Qfalse); + + rb_hash_aset(state, rb_str_new2("json_opts"), json_opts); + + rb_yajl_gen = Data_Wrap_Struct(cYajl_Gen, NULL, NULL, yajl_gen); + + rb_funcall(obj, sym_ffi_yajl, 2, rb_yajl_gen, state); + + yajl_gen_get_buf(yajl_gen, &buf, &len); + + ret = rb_str_new2((char *)buf); + + yajl_gen_free(yajl_gen); + + return ret; +} + +int rb_cHash_ffi_yajl_callback(VALUE key, VALUE val, VALUE extra) { + ID sym_ffi_yajl = rb_intern("ffi_yajl"); + VALUE state = rb_hash_aref(extra, rb_str_new2("state")); + VALUE rb_yajl_gen = rb_hash_aref(extra, rb_str_new2("yajl_gen")); + + rb_hash_aset(state, rb_str_new2("processing_key"), Qtrue); + rb_funcall(key, sym_ffi_yajl, 2, rb_yajl_gen, state); + rb_hash_aset(state, rb_str_new2("processing_key"), Qfalse); + + rb_funcall(val, sym_ffi_yajl, 2, rb_yajl_gen, state); + + return 0; +} + +#define RB_FUNC0(call) rb_funcall(self, rb_intern(call), 0) + +/* + * wrappers around yajl_gen_* functions + */ + +/* encode a c-string as a yajl string */ +VALUE gen_cstring(VALUE rb_yajl_gen, char *cptr, int len) { + yajl_gen_status status; + struct yajl_gen_t *yajl_gen; + Data_Get_Struct(rb_yajl_gen, struct yajl_gen_t, yajl_gen); + + if ((status = yajl_gen_string(yajl_gen, (unsigned char *)cptr, len)) != yajl_gen_status_ok) { + rb_funcall(mEncoder2, rb_intern("raise_error_for_status"), 2, INT2FIX(status), rb_str_new(cptr, len)); + } + + return Qnil; +} + +/* encode a ruby-sring as a yajl string */ +VALUE gen_string(VALUE rb_yajl_gen, VALUE str) { + char *cptr = RSTRING_PTR(str); + int len = RSTRING_LEN(str); + + return gen_cstring(rb_yajl_gen, cptr, len); +} + +/* calls #to_s on an object to encode it as a yajl string */ +static VALUE gen_string_to_s(VALUE rb_yajl_gen, VALUE self) { + return gen_string(rb_yajl_gen, RB_FUNC0("to_s")); +} + +/* encode a ruby string as a yajl number (also used to embed already-rendered json from #to_json) */ +VALUE gen_number(VALUE rb_yajl_gen, VALUE str) { + yajl_gen_status status; + struct yajl_gen_t *yajl_gen; + Data_Get_Struct(rb_yajl_gen, struct yajl_gen_t, yajl_gen); + char *cptr = RSTRING_PTR(str); + int len = RSTRING_LEN(str); + + if ((status = yajl_gen_number(yajl_gen, cptr, len)) != yajl_gen_status_ok) { + rb_funcall(mEncoder2, rb_intern("raise_error_for_status"), 2, INT2FIX(status), str); + } + + return Qnil; +} + +/* encode hash open */ +VALUE gen_map_open(VALUE rb_yajl_gen) { + yajl_gen_status status; + struct yajl_gen_t *yajl_gen; + Data_Get_Struct(rb_yajl_gen, struct yajl_gen_t, yajl_gen); + + if ((status = yajl_gen_map_open(yajl_gen)) != yajl_gen_status_ok) { + rb_funcall(mEncoder2, rb_intern("raise_error_for_status"), 2, INT2FIX(status), rb_str_new2("{")); + } + + return Qnil; +} + +/* encode a hash close */ +VALUE gen_map_close(VALUE rb_yajl_gen) { + yajl_gen_status status; + struct yajl_gen_t *yajl_gen; + Data_Get_Struct(rb_yajl_gen, struct yajl_gen_t, yajl_gen); + + if ((status = yajl_gen_map_close(yajl_gen)) != yajl_gen_status_ok) { + rb_funcall(mEncoder2, rb_intern("raise_error_for_status"), 2, INT2FIX(status), rb_str_new2("}")); + } + + return Qnil; +} + +/* encode an array open */ +VALUE gen_array_open(VALUE rb_yajl_gen) { + yajl_gen_status status; + struct yajl_gen_t *yajl_gen; + Data_Get_Struct(rb_yajl_gen, struct yajl_gen_t, yajl_gen); + + if ((status = yajl_gen_array_open(yajl_gen)) != yajl_gen_status_ok) { + rb_funcall(mEncoder2, rb_intern("raise_error_for_status"), 2, INT2FIX(status), rb_str_new2("[")); + } + + return Qnil; +} + +/* encode an array close */ +VALUE gen_array_close(VALUE rb_yajl_gen) { + yajl_gen_status status; + struct yajl_gen_t *yajl_gen; + Data_Get_Struct(rb_yajl_gen, struct yajl_gen_t, yajl_gen); + + if ((status = yajl_gen_array_close(yajl_gen)) != yajl_gen_status_ok) { + rb_funcall(mEncoder2, rb_intern("raise_error_for_status"), 2, INT2FIX(status), rb_str_new2("]")); + } + + return Qnil; +} + +/* encode a json null */ +VALUE gen_null(VALUE rb_yajl_gen) { + yajl_gen_status status; + struct yajl_gen_t *yajl_gen; + Data_Get_Struct(rb_yajl_gen, struct yajl_gen_t, yajl_gen); + + if ((status = yajl_gen_null(yajl_gen)) != yajl_gen_status_ok) { + rb_funcall(mEncoder2, rb_intern("raise_error_for_status"), 2, INT2FIX(status), rb_str_new2("null")); + } + + return Qnil; +} + +/* encode a true value */ +VALUE gen_true(VALUE rb_yajl_gen) { + yajl_gen_status status; + struct yajl_gen_t *yajl_gen; + Data_Get_Struct(rb_yajl_gen, struct yajl_gen_t, yajl_gen); + + if ((status = yajl_gen_bool(yajl_gen, 1)) != yajl_gen_status_ok) { + rb_funcall(mEncoder2, rb_intern("raise_error_for_status"), 2, INT2FIX(status), rb_str_new2("true")); + } + + return Qnil; +} + +/* encode a false value */ +VALUE gen_false(VALUE rb_yajl_gen) { + yajl_gen_status status; + struct yajl_gen_t *yajl_gen; + Data_Get_Struct(rb_yajl_gen, struct yajl_gen_t, yajl_gen); + + if ((status = yajl_gen_bool(yajl_gen, 0)) != yajl_gen_status_ok) { + rb_funcall(mEncoder2, rb_intern("raise_error_for_status"), 2, INT2FIX(status), rb_str_new2("false")); + } + + return Qnil; +} + +/* + * #to_ffi_yajl() method calls + */ + +static VALUE rb_cHash_ffi_yajl(VALUE self, VALUE rb_yajl_gen, VALUE state) { + if ( rb_hash_aref(state, rb_str_new2("processing_key")) == Qtrue ) { + gen_string(rb_yajl_gen, rb_funcall(self, rb_intern("to_s"), 0)); + } else { + + /* FIXME: i think this got refactored from something else and it is now pointless -- + should just pass rb_yajl_gen directly instead of this 'extra' hash -- i don't + *think* this indirection is doing anything useful to mark memory against the GC */ + + VALUE extra = rb_hash_new(); + + rb_hash_aset(extra, rb_str_new2("yajl_gen"), rb_yajl_gen); + + rb_hash_aset(extra, rb_str_new2("state"), state); + + gen_map_open(rb_yajl_gen); + + rb_hash_foreach(self, rb_cHash_ffi_yajl_callback, extra); + + gen_map_close(rb_yajl_gen); + } + + return Qnil; +} + +static VALUE rb_cArray_ffi_yajl(VALUE self, VALUE rb_yajl_gen, VALUE state) { + if ( rb_hash_aref(state, rb_str_new2("processing_key")) == Qtrue ) { + gen_string(rb_yajl_gen, rb_funcall(self, rb_intern("to_s"), 0)); + } else { + VALUE val; + long i; + ID sym_ffi_yajl = rb_intern("ffi_yajl"); + + gen_array_open(rb_yajl_gen); + + for(i=0; i +#include + +#ifdef HAVE_RUBY_ENCODING_H +#include +static rb_encoding *utf8Encoding; +#endif + +static VALUE mFFI_Yajl, mExt, mParser, cParseError; + +typedef struct { + VALUE self; + int symbolizeKeys; + int uniqueKeyChecking; +} CTX; + +void set_value(CTX *ctx, VALUE val) { + VALUE stack = rb_ivar_get(ctx->self, rb_intern("stack")); + VALUE key = rb_ivar_get(ctx->self, rb_intern("key")); + long len = RARRAY_LEN(stack); + VALUE last = rb_ary_entry(stack, len-1); + switch (TYPE(last)) { + case T_ARRAY: + rb_ary_push(last, val); + break; + case T_HASH: + if ( ctx->uniqueKeyChecking ) { + ID sym_has_key = rb_intern("has_key?"); + if ( rb_funcall(last, sym_has_key, 1, key) == Qtrue ) { + rb_raise(cParseError, "repeated key: %s", RSTRING_PTR(key)); + } + } + rb_hash_aset(last, key, val); + break; + default: + rb_ary_push(stack, val); + break; + } +} + +void set_key(CTX *ctx, VALUE key) { + rb_ivar_set(ctx->self, rb_intern("key"), key); +} + +void start_object(CTX *ctx, VALUE obj) { + VALUE key_stack = rb_ivar_get(ctx->self, rb_intern("key_stack")); + VALUE key = rb_ivar_get(ctx->self, rb_intern("key")); + VALUE stack = rb_ivar_get(ctx->self, rb_intern("stack")); + + rb_ary_push(key_stack, key); + rb_ary_push(stack, obj); +} + +void end_object(CTX *ctx) { + VALUE key_stack = rb_ivar_get(ctx->self, rb_intern("key_stack")); + VALUE stack = rb_ivar_get(ctx->self, rb_intern("stack")); + rb_ivar_set(ctx->self, rb_intern("key"), rb_ary_pop(key_stack)); + if ( RARRAY_LEN(stack) > 1 ) { + set_value(ctx, rb_ary_pop(stack)); + } +} + +int null_callback(void *ctx) { + set_value(ctx, Qnil); + return 1; +} + +int boolean_callback(void *ctx, int boolean) { + set_value(ctx, boolean ? Qtrue : Qfalse); + return 1; +} + +int integer_callback(void *ctx, long long intVal) { + set_value(ctx, LONG2NUM(intVal)); + return 1; +} + +int double_callback(void *ctx, double doubleVal) { + set_value(ctx, rb_float_new(doubleVal)); + return 1; +} + +int number_callback(void *ctx, const char *numberVal, size_t numberLen) { + char *buf = (char *)malloc(numberLen+1); + buf[numberLen] = 0; + memcpy(buf, numberVal, numberLen); + if (memchr(buf, '.', numberLen) || + memchr(buf, 'e', numberLen) || + memchr(buf, 'E', numberLen)) { + set_value(ctx, rb_float_new(strtod(buf, NULL))); + } else { + set_value(ctx, rb_cstr2inum(buf, 10)); + } + free(buf); + return 1; +} + +int string_callback(void *ctx, const unsigned char *stringVal, size_t stringLen) { + VALUE str = rb_str_new((const char *)stringVal, stringLen); +#ifdef HAVE_RUBY_ENCODING_H + rb_encoding *default_internal_enc = rb_default_internal_encoding(); + rb_enc_associate(str, utf8Encoding); + if (default_internal_enc) { + str = rb_str_export_to_enc(str, default_internal_enc); + } +#endif + set_value(ctx,str); + return 1; +} + +int start_map_callback(void *ctx) { + start_object(ctx,rb_hash_new()); + return 1; +} + +int map_key_callback(void *ctx, const unsigned char *stringVal, size_t stringLen) { + VALUE key; +#ifdef HAVE_RUBY_ENCODING_H + rb_encoding *default_internal_enc; +#endif + + if ( ((CTX *)ctx)->symbolizeKeys ) { +#ifdef HAVE_RUBY_ENCODING_H + ID id = rb_intern3((const char *)stringVal, stringLen, utf8Encoding); + key = ID2SYM(id); +#else + VALUE str = rb_str_new((const char *)stringVal, stringLen); + key = rb_str_intern(str); +#endif + } else { + key = rb_str_new((const char *)stringVal, stringLen); +#ifdef HAVE_RUBY_ENCODING_H + default_internal_enc = rb_default_internal_encoding(); + rb_enc_associate(key, utf8Encoding); + if (default_internal_enc) { + key = rb_str_export_to_enc(key, default_internal_enc); + } +#endif + } + set_key(ctx, key); + return 1; +} + +int end_map_callback(void *ctx) { + end_object(ctx); + return 1; +} + +int start_array_callback(void *ctx) { + start_object(ctx,rb_ary_new()); + return 1; +} + +int end_array_callback(void *ctx) { + end_object(ctx); + return 1; +} + +static yajl_callbacks callbacks = { + null_callback, + boolean_callback, + integer_callback, + double_callback, + number_callback, + string_callback, + start_map_callback, + map_key_callback, + end_map_callback, + start_array_callback, + end_array_callback, +}; + +int get_opts_key(VALUE self, const char *key) { + VALUE opts = rb_iv_get(self, "@opts"); + if (TYPE(opts) != T_HASH) { + rb_raise(rb_eTypeError, "opts is not a valid hash"); + } + return rb_hash_aref(opts, ID2SYM(rb_intern(key))) == Qtrue; +} + +static VALUE mParser_do_yajl_parse(VALUE self, VALUE str, VALUE yajl_opts) { + yajl_handle hand; + yajl_status stat; + unsigned char *err; + volatile CTX ctx; + + rb_ivar_set(self, rb_intern("key"), Qnil); + rb_ivar_set(self, rb_intern("stack"), rb_ary_new()); + rb_ivar_set(self, rb_intern("key_stack"), rb_ary_new()); + + ctx.self = self; + ctx.symbolizeKeys = get_opts_key(self, "symbolize_keys"); + ctx.uniqueKeyChecking = get_opts_key(self, "unique_key_checking"); + + hand = yajl_alloc(&callbacks, NULL, (void *)&ctx); + + if (rb_hash_aref(yajl_opts, ID2SYM(rb_intern("yajl_allow_comments"))) == Qtrue) { + yajl_config(hand, yajl_allow_comments, 1); + } + if (rb_hash_aref(yajl_opts, ID2SYM(rb_intern("yajl_dont_validate_strings"))) == Qtrue) { + yajl_config(hand, yajl_dont_validate_strings, 1); + } + if (rb_hash_aref(yajl_opts, ID2SYM(rb_intern("yajl_allow_trailing_garbage"))) == Qtrue) { + yajl_config(hand, yajl_allow_trailing_garbage, 1); + } + if (rb_hash_aref(yajl_opts, ID2SYM(rb_intern("yajl_allow_multiple_values"))) == Qtrue) { + yajl_config(hand, yajl_allow_multiple_values, 1); + } + if (rb_hash_aref(yajl_opts, ID2SYM(rb_intern("yajl_allow_partial_values"))) == Qtrue) { + yajl_config(hand, yajl_allow_partial_values, 1); + } + + if ((stat = yajl_parse(hand, (unsigned char *)RSTRING_PTR(str), RSTRING_LEN(str))) != yajl_status_ok) { + err = yajl_get_error(hand, 1, (unsigned char *)RSTRING_PTR(str), RSTRING_LEN(str)); + goto raise; + } + if ((stat = yajl_complete_parse(hand)) != yajl_status_ok) { + err = yajl_get_error(hand, 1, (unsigned char *)RSTRING_PTR(str), RSTRING_LEN(str)); + goto raise; + } + yajl_free(hand); + return rb_ary_pop(rb_ivar_get(self, rb_intern("stack"))); + +raise: + if (hand) { + yajl_free(hand); + } + rb_raise(cParseError, "%s", err); +} + +void Init_parser() { + mFFI_Yajl = rb_define_module("FFI_Yajl"); + cParseError = rb_define_class_under(mFFI_Yajl, "ParseError", rb_eStandardError); + mExt = rb_define_module_under(mFFI_Yajl, "Ext"); + mParser = rb_define_module_under(mExt, "Parser"); + rb_define_method(mParser, "do_yajl_parse", mParser_do_yajl_parse, 2); +#ifdef HAVE_RUBY_ENCODING_H + utf8Encoding = rb_utf8_encoding(); +#endif +} -- cgit v1.2.1 From 96dedf57cf2f92049a5884896fd2447396d0bec9 Mon Sep 17 00:00:00 2001 From: skeshari12 Date: Tue, 12 Apr 2022 17:33:05 +0530 Subject: fix error Signed-off-by: skeshari12 --- ext/dlopen/dlopen.c | 40 ---- ext/dlopen/extconf.rb | 16 -- ext/encoder/encoder.c | 394 --------------------------------- ext/encoder/extconf.rb | 65 ------ ext/parser/extconf.rb | 65 ------ ext/parser/parser.c | 240 -------------------- spec/ffi_yajl/encoder_spec.rb | 1 + spec/ffi_yajl/map_library_name_spec.rb | 1 + spec/ffi_yajl/parser_spec.rb | 1 + 9 files changed, 3 insertions(+), 820 deletions(-) delete mode 100644 ext/dlopen/dlopen.c delete mode 100644 ext/dlopen/extconf.rb delete mode 100644 ext/encoder/encoder.c delete mode 100644 ext/encoder/extconf.rb delete mode 100644 ext/parser/extconf.rb delete mode 100644 ext/parser/parser.c diff --git a/ext/dlopen/dlopen.c b/ext/dlopen/dlopen.c deleted file mode 100644 index eb81a21..0000000 --- a/ext/dlopen/dlopen.c +++ /dev/null @@ -1,40 +0,0 @@ -#include - -#if defined(HAVE_DLFCN_H) -# include -#ifndef RTLD_LAZY -#define RTLD_LAZY 0 -#endif -#ifndef RTLD_GLOBAL -#define RTLD_GLOBAL 0 -#endif -#ifndef RTLD_NOW -#define RTLD_NOW 0 -#endif -#else -# if defined(_WIN32) -# include -# define dlopen(name,flag) ((void*)LoadLibrary(name)) -# define dlerror() strerror(rb_w32_map_errno(GetLastError())) -# define dlsym(handle,name) ((void*)GetProcAddress((handle),(name))) -# define RTLD_LAZY -1 -# define RTLD_NOW -1 -# define RTLD_GLOBAL -1 -# endif -#endif - -static VALUE mFFI_Yajl, mDlopen, mExt; - -static VALUE mDlopen_dlopen(VALUE self, VALUE file) { - if (dlopen(RSTRING_PTR(file), RTLD_NOW|RTLD_GLOBAL) == NULL) { - rb_raise(rb_eArgError, "%s", dlerror()); - } - return Qnil; -} - -void Init_dlopen() { - mFFI_Yajl = rb_define_module("FFI_Yajl"); - mExt = rb_define_module_under(mFFI_Yajl, "Ext"); - mDlopen = rb_define_module_under(mExt, "Dlopen"); - rb_define_method(mDlopen, "dlopen", mDlopen_dlopen, 1); -} diff --git a/ext/dlopen/extconf.rb b/ext/dlopen/extconf.rb deleted file mode 100644 index 803d326..0000000 --- a/ext/dlopen/extconf.rb +++ /dev/null @@ -1,16 +0,0 @@ -# rubocop:disable Style/GlobalVars -require "mkmf" -require "rubygems" - -RbConfig::MAKEFILE_CONFIG["CC"] = ENV["CC"] if ENV["CC"] - -puts $CFLAGS -puts $LDFLAGS - -have_header("dlfcn.h") - -have_library("dl", "dlopen") - -dir_config "dlopen" - -create_makefile "ffi_yajl/ext/dlopen" diff --git a/ext/encoder/encoder.c b/ext/encoder/encoder.c deleted file mode 100644 index 6f1492c..0000000 --- a/ext/encoder/encoder.c +++ /dev/null @@ -1,394 +0,0 @@ -#include -#include - -static VALUE mFFI_Yajl, mExt, mEncoder, mEncoder2, cEncodeError; -static VALUE cDate, cTime, cDateTime, cStringIO; -static VALUE cYajl_Gen; - -/* FIXME: the json gem does a whole bunch of indirection around monkeypatching... not sure if we need to as well... */ - -static VALUE mEncoder_do_yajl_encode(VALUE self, VALUE obj, VALUE yajl_gen_opts, VALUE json_opts) { - ID sym_ffi_yajl = rb_intern("ffi_yajl"); - VALUE sym_yajl_gen_beautify = ID2SYM(rb_intern("yajl_gen_beautify")); - VALUE sym_yajl_gen_validate_utf8 = ID2SYM(rb_intern("yajl_gen_validate_utf8")); - VALUE sym_yajl_gen_indent_string = ID2SYM(rb_intern("yajl_gen_indent_string")); - yajl_gen yajl_gen; - const unsigned char *buf; - size_t len; - VALUE state; - VALUE ret; - VALUE indent_string; - VALUE rb_yajl_gen; - - yajl_gen = yajl_gen_alloc(NULL); - - if ( rb_hash_aref(yajl_gen_opts, sym_yajl_gen_beautify) == Qtrue ) { - yajl_gen_config(yajl_gen, yajl_gen_beautify, 1); - } - if ( rb_hash_aref(yajl_gen_opts, sym_yajl_gen_validate_utf8) == Qtrue ) { - yajl_gen_config(yajl_gen, yajl_gen_validate_utf8, 1); - } - - indent_string = rb_hash_aref(yajl_gen_opts, sym_yajl_gen_indent_string); - if (indent_string != Qnil) { - yajl_gen_config(yajl_gen, yajl_gen_indent_string, RSTRING_PTR(indent_string)); - } else { - yajl_gen_config(yajl_gen, yajl_gen_indent_string, " "); - } - - state = rb_hash_new(); - - rb_hash_aset(state, rb_str_new2("processing_key"), Qfalse); - - rb_hash_aset(state, rb_str_new2("json_opts"), json_opts); - - rb_yajl_gen = Data_Wrap_Struct(cYajl_Gen, NULL, NULL, yajl_gen); - - rb_funcall(obj, sym_ffi_yajl, 2, rb_yajl_gen, state); - - yajl_gen_get_buf(yajl_gen, &buf, &len); - - ret = rb_str_new2((char *)buf); - - yajl_gen_free(yajl_gen); - - return ret; -} - -int rb_cHash_ffi_yajl_callback(VALUE key, VALUE val, VALUE extra) { - ID sym_ffi_yajl = rb_intern("ffi_yajl"); - VALUE state = rb_hash_aref(extra, rb_str_new2("state")); - VALUE rb_yajl_gen = rb_hash_aref(extra, rb_str_new2("yajl_gen")); - - rb_hash_aset(state, rb_str_new2("processing_key"), Qtrue); - rb_funcall(key, sym_ffi_yajl, 2, rb_yajl_gen, state); - rb_hash_aset(state, rb_str_new2("processing_key"), Qfalse); - - rb_funcall(val, sym_ffi_yajl, 2, rb_yajl_gen, state); - - return 0; -} - -#define RB_FUNC0(call) rb_funcall(self, rb_intern(call), 0) - -/* - * wrappers around yajl_gen_* functions - */ - -/* encode a c-string as a yajl string */ -VALUE gen_cstring(VALUE rb_yajl_gen, char *cptr, int len) { - yajl_gen_status status; - struct yajl_gen_t *yajl_gen; - Data_Get_Struct(rb_yajl_gen, struct yajl_gen_t, yajl_gen); - - if ((status = yajl_gen_string(yajl_gen, (unsigned char *)cptr, len)) != yajl_gen_status_ok) { - rb_funcall(mEncoder2, rb_intern("raise_error_for_status"), 2, INT2FIX(status), rb_str_new(cptr, len)); - } - - return Qnil; -} - -/* encode a ruby-sring as a yajl string */ -VALUE gen_string(VALUE rb_yajl_gen, VALUE str) { - char *cptr = RSTRING_PTR(str); - int len = RSTRING_LEN(str); - - return gen_cstring(rb_yajl_gen, cptr, len); -} - -/* calls #to_s on an object to encode it as a yajl string */ -static VALUE gen_string_to_s(VALUE rb_yajl_gen, VALUE self) { - return gen_string(rb_yajl_gen, RB_FUNC0("to_s")); -} - -/* encode a ruby string as a yajl number (also used to embed already-rendered json from #to_json) */ -VALUE gen_number(VALUE rb_yajl_gen, VALUE str) { - yajl_gen_status status; - struct yajl_gen_t *yajl_gen; - Data_Get_Struct(rb_yajl_gen, struct yajl_gen_t, yajl_gen); - char *cptr = RSTRING_PTR(str); - int len = RSTRING_LEN(str); - - if ((status = yajl_gen_number(yajl_gen, cptr, len)) != yajl_gen_status_ok) { - rb_funcall(mEncoder2, rb_intern("raise_error_for_status"), 2, INT2FIX(status), str); - } - - return Qnil; -} - -/* encode hash open */ -VALUE gen_map_open(VALUE rb_yajl_gen) { - yajl_gen_status status; - struct yajl_gen_t *yajl_gen; - Data_Get_Struct(rb_yajl_gen, struct yajl_gen_t, yajl_gen); - - if ((status = yajl_gen_map_open(yajl_gen)) != yajl_gen_status_ok) { - rb_funcall(mEncoder2, rb_intern("raise_error_for_status"), 2, INT2FIX(status), rb_str_new2("{")); - } - - return Qnil; -} - -/* encode a hash close */ -VALUE gen_map_close(VALUE rb_yajl_gen) { - yajl_gen_status status; - struct yajl_gen_t *yajl_gen; - Data_Get_Struct(rb_yajl_gen, struct yajl_gen_t, yajl_gen); - - if ((status = yajl_gen_map_close(yajl_gen)) != yajl_gen_status_ok) { - rb_funcall(mEncoder2, rb_intern("raise_error_for_status"), 2, INT2FIX(status), rb_str_new2("}")); - } - - return Qnil; -} - -/* encode an array open */ -VALUE gen_array_open(VALUE rb_yajl_gen) { - yajl_gen_status status; - struct yajl_gen_t *yajl_gen; - Data_Get_Struct(rb_yajl_gen, struct yajl_gen_t, yajl_gen); - - if ((status = yajl_gen_array_open(yajl_gen)) != yajl_gen_status_ok) { - rb_funcall(mEncoder2, rb_intern("raise_error_for_status"), 2, INT2FIX(status), rb_str_new2("[")); - } - - return Qnil; -} - -/* encode an array close */ -VALUE gen_array_close(VALUE rb_yajl_gen) { - yajl_gen_status status; - struct yajl_gen_t *yajl_gen; - Data_Get_Struct(rb_yajl_gen, struct yajl_gen_t, yajl_gen); - - if ((status = yajl_gen_array_close(yajl_gen)) != yajl_gen_status_ok) { - rb_funcall(mEncoder2, rb_intern("raise_error_for_status"), 2, INT2FIX(status), rb_str_new2("]")); - } - - return Qnil; -} - -/* encode a json null */ -VALUE gen_null(VALUE rb_yajl_gen) { - yajl_gen_status status; - struct yajl_gen_t *yajl_gen; - Data_Get_Struct(rb_yajl_gen, struct yajl_gen_t, yajl_gen); - - if ((status = yajl_gen_null(yajl_gen)) != yajl_gen_status_ok) { - rb_funcall(mEncoder2, rb_intern("raise_error_for_status"), 2, INT2FIX(status), rb_str_new2("null")); - } - - return Qnil; -} - -/* encode a true value */ -VALUE gen_true(VALUE rb_yajl_gen) { - yajl_gen_status status; - struct yajl_gen_t *yajl_gen; - Data_Get_Struct(rb_yajl_gen, struct yajl_gen_t, yajl_gen); - - if ((status = yajl_gen_bool(yajl_gen, 1)) != yajl_gen_status_ok) { - rb_funcall(mEncoder2, rb_intern("raise_error_for_status"), 2, INT2FIX(status), rb_str_new2("true")); - } - - return Qnil; -} - -/* encode a false value */ -VALUE gen_false(VALUE rb_yajl_gen) { - yajl_gen_status status; - struct yajl_gen_t *yajl_gen; - Data_Get_Struct(rb_yajl_gen, struct yajl_gen_t, yajl_gen); - - if ((status = yajl_gen_bool(yajl_gen, 0)) != yajl_gen_status_ok) { - rb_funcall(mEncoder2, rb_intern("raise_error_for_status"), 2, INT2FIX(status), rb_str_new2("false")); - } - - return Qnil; -} - -/* - * #to_ffi_yajl() method calls - */ - -static VALUE rb_cHash_ffi_yajl(VALUE self, VALUE rb_yajl_gen, VALUE state) { - if ( rb_hash_aref(state, rb_str_new2("processing_key")) == Qtrue ) { - gen_string(rb_yajl_gen, rb_funcall(self, rb_intern("to_s"), 0)); - } else { - - /* FIXME: i think this got refactored from something else and it is now pointless -- - should just pass rb_yajl_gen directly instead of this 'extra' hash -- i don't - *think* this indirection is doing anything useful to mark memory against the GC */ - - VALUE extra = rb_hash_new(); - - rb_hash_aset(extra, rb_str_new2("yajl_gen"), rb_yajl_gen); - - rb_hash_aset(extra, rb_str_new2("state"), state); - - gen_map_open(rb_yajl_gen); - - rb_hash_foreach(self, rb_cHash_ffi_yajl_callback, extra); - - gen_map_close(rb_yajl_gen); - } - - return Qnil; -} - -static VALUE rb_cArray_ffi_yajl(VALUE self, VALUE rb_yajl_gen, VALUE state) { - if ( rb_hash_aref(state, rb_str_new2("processing_key")) == Qtrue ) { - gen_string(rb_yajl_gen, rb_funcall(self, rb_intern("to_s"), 0)); - } else { - VALUE val; - long i; - ID sym_ffi_yajl = rb_intern("ffi_yajl"); - - gen_array_open(rb_yajl_gen); - - for(i=0; i -#include - -#ifdef HAVE_RUBY_ENCODING_H -#include -static rb_encoding *utf8Encoding; -#endif - -static VALUE mFFI_Yajl, mExt, mParser, cParseError; - -typedef struct { - VALUE self; - int symbolizeKeys; - int uniqueKeyChecking; -} CTX; - -void set_value(CTX *ctx, VALUE val) { - VALUE stack = rb_ivar_get(ctx->self, rb_intern("stack")); - VALUE key = rb_ivar_get(ctx->self, rb_intern("key")); - long len = RARRAY_LEN(stack); - VALUE last = rb_ary_entry(stack, len-1); - switch (TYPE(last)) { - case T_ARRAY: - rb_ary_push(last, val); - break; - case T_HASH: - if ( ctx->uniqueKeyChecking ) { - ID sym_has_key = rb_intern("has_key?"); - if ( rb_funcall(last, sym_has_key, 1, key) == Qtrue ) { - rb_raise(cParseError, "repeated key: %s", RSTRING_PTR(key)); - } - } - rb_hash_aset(last, key, val); - break; - default: - rb_ary_push(stack, val); - break; - } -} - -void set_key(CTX *ctx, VALUE key) { - rb_ivar_set(ctx->self, rb_intern("key"), key); -} - -void start_object(CTX *ctx, VALUE obj) { - VALUE key_stack = rb_ivar_get(ctx->self, rb_intern("key_stack")); - VALUE key = rb_ivar_get(ctx->self, rb_intern("key")); - VALUE stack = rb_ivar_get(ctx->self, rb_intern("stack")); - - rb_ary_push(key_stack, key); - rb_ary_push(stack, obj); -} - -void end_object(CTX *ctx) { - VALUE key_stack = rb_ivar_get(ctx->self, rb_intern("key_stack")); - VALUE stack = rb_ivar_get(ctx->self, rb_intern("stack")); - rb_ivar_set(ctx->self, rb_intern("key"), rb_ary_pop(key_stack)); - if ( RARRAY_LEN(stack) > 1 ) { - set_value(ctx, rb_ary_pop(stack)); - } -} - -int null_callback(void *ctx) { - set_value(ctx, Qnil); - return 1; -} - -int boolean_callback(void *ctx, int boolean) { - set_value(ctx, boolean ? Qtrue : Qfalse); - return 1; -} - -int integer_callback(void *ctx, long long intVal) { - set_value(ctx, LONG2NUM(intVal)); - return 1; -} - -int double_callback(void *ctx, double doubleVal) { - set_value(ctx, rb_float_new(doubleVal)); - return 1; -} - -int number_callback(void *ctx, const char *numberVal, size_t numberLen) { - char *buf = (char *)malloc(numberLen+1); - buf[numberLen] = 0; - memcpy(buf, numberVal, numberLen); - if (memchr(buf, '.', numberLen) || - memchr(buf, 'e', numberLen) || - memchr(buf, 'E', numberLen)) { - set_value(ctx, rb_float_new(strtod(buf, NULL))); - } else { - set_value(ctx, rb_cstr2inum(buf, 10)); - } - free(buf); - return 1; -} - -int string_callback(void *ctx, const unsigned char *stringVal, size_t stringLen) { - VALUE str = rb_str_new((const char *)stringVal, stringLen); -#ifdef HAVE_RUBY_ENCODING_H - rb_encoding *default_internal_enc = rb_default_internal_encoding(); - rb_enc_associate(str, utf8Encoding); - if (default_internal_enc) { - str = rb_str_export_to_enc(str, default_internal_enc); - } -#endif - set_value(ctx,str); - return 1; -} - -int start_map_callback(void *ctx) { - start_object(ctx,rb_hash_new()); - return 1; -} - -int map_key_callback(void *ctx, const unsigned char *stringVal, size_t stringLen) { - VALUE key; -#ifdef HAVE_RUBY_ENCODING_H - rb_encoding *default_internal_enc; -#endif - - if ( ((CTX *)ctx)->symbolizeKeys ) { -#ifdef HAVE_RUBY_ENCODING_H - ID id = rb_intern3((const char *)stringVal, stringLen, utf8Encoding); - key = ID2SYM(id); -#else - VALUE str = rb_str_new((const char *)stringVal, stringLen); - key = rb_str_intern(str); -#endif - } else { - key = rb_str_new((const char *)stringVal, stringLen); -#ifdef HAVE_RUBY_ENCODING_H - default_internal_enc = rb_default_internal_encoding(); - rb_enc_associate(key, utf8Encoding); - if (default_internal_enc) { - key = rb_str_export_to_enc(key, default_internal_enc); - } -#endif - } - set_key(ctx, key); - return 1; -} - -int end_map_callback(void *ctx) { - end_object(ctx); - return 1; -} - -int start_array_callback(void *ctx) { - start_object(ctx,rb_ary_new()); - return 1; -} - -int end_array_callback(void *ctx) { - end_object(ctx); - return 1; -} - -static yajl_callbacks callbacks = { - null_callback, - boolean_callback, - integer_callback, - double_callback, - number_callback, - string_callback, - start_map_callback, - map_key_callback, - end_map_callback, - start_array_callback, - end_array_callback, -}; - -int get_opts_key(VALUE self, const char *key) { - VALUE opts = rb_iv_get(self, "@opts"); - if (TYPE(opts) != T_HASH) { - rb_raise(rb_eTypeError, "opts is not a valid hash"); - } - return rb_hash_aref(opts, ID2SYM(rb_intern(key))) == Qtrue; -} - -static VALUE mParser_do_yajl_parse(VALUE self, VALUE str, VALUE yajl_opts) { - yajl_handle hand; - yajl_status stat; - unsigned char *err; - volatile CTX ctx; - - rb_ivar_set(self, rb_intern("key"), Qnil); - rb_ivar_set(self, rb_intern("stack"), rb_ary_new()); - rb_ivar_set(self, rb_intern("key_stack"), rb_ary_new()); - - ctx.self = self; - ctx.symbolizeKeys = get_opts_key(self, "symbolize_keys"); - ctx.uniqueKeyChecking = get_opts_key(self, "unique_key_checking"); - - hand = yajl_alloc(&callbacks, NULL, (void *)&ctx); - - if (rb_hash_aref(yajl_opts, ID2SYM(rb_intern("yajl_allow_comments"))) == Qtrue) { - yajl_config(hand, yajl_allow_comments, 1); - } - if (rb_hash_aref(yajl_opts, ID2SYM(rb_intern("yajl_dont_validate_strings"))) == Qtrue) { - yajl_config(hand, yajl_dont_validate_strings, 1); - } - if (rb_hash_aref(yajl_opts, ID2SYM(rb_intern("yajl_allow_trailing_garbage"))) == Qtrue) { - yajl_config(hand, yajl_allow_trailing_garbage, 1); - } - if (rb_hash_aref(yajl_opts, ID2SYM(rb_intern("yajl_allow_multiple_values"))) == Qtrue) { - yajl_config(hand, yajl_allow_multiple_values, 1); - } - if (rb_hash_aref(yajl_opts, ID2SYM(rb_intern("yajl_allow_partial_values"))) == Qtrue) { - yajl_config(hand, yajl_allow_partial_values, 1); - } - - if ((stat = yajl_parse(hand, (unsigned char *)RSTRING_PTR(str), RSTRING_LEN(str))) != yajl_status_ok) { - err = yajl_get_error(hand, 1, (unsigned char *)RSTRING_PTR(str), RSTRING_LEN(str)); - goto raise; - } - if ((stat = yajl_complete_parse(hand)) != yajl_status_ok) { - err = yajl_get_error(hand, 1, (unsigned char *)RSTRING_PTR(str), RSTRING_LEN(str)); - goto raise; - } - yajl_free(hand); - return rb_ary_pop(rb_ivar_get(self, rb_intern("stack"))); - -raise: - if (hand) { - yajl_free(hand); - } - rb_raise(cParseError, "%s", err); -} - -void Init_parser() { - mFFI_Yajl = rb_define_module("FFI_Yajl"); - cParseError = rb_define_class_under(mFFI_Yajl, "ParseError", rb_eStandardError); - mExt = rb_define_module_under(mFFI_Yajl, "Ext"); - mParser = rb_define_module_under(mExt, "Parser"); - rb_define_method(mParser, "do_yajl_parse", mParser_do_yajl_parse, 2); -#ifdef HAVE_RUBY_ENCODING_H - utf8Encoding = rb_utf8_encoding(); -#endif -} diff --git a/spec/ffi_yajl/encoder_spec.rb b/spec/ffi_yajl/encoder_spec.rb index 7a76c0a..d2612a5 100644 --- a/spec/ffi_yajl/encoder_spec.rb +++ b/spec/ffi_yajl/encoder_spec.rb @@ -23,6 +23,7 @@ require "spec_helper" require "date" +require "ffi_yajl/ext/dlopen" describe "FFI_Yajl::Encoder" do let(:options) { {} } diff --git a/spec/ffi_yajl/map_library_name_spec.rb b/spec/ffi_yajl/map_library_name_spec.rb index 6ec8bb6..e63fadc 100644 --- a/spec/ffi_yajl/map_library_name_spec.rb +++ b/spec/ffi_yajl/map_library_name_spec.rb @@ -21,6 +21,7 @@ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. require "spec_helper" +require "ffi_yajl/ext/dlopen" class Test extend FFI_Yajl::MapLibraryName diff --git a/spec/ffi_yajl/parser_spec.rb b/spec/ffi_yajl/parser_spec.rb index 239ae26..9ac924a 100644 --- a/spec/ffi_yajl/parser_spec.rb +++ b/spec/ffi_yajl/parser_spec.rb @@ -22,6 +22,7 @@ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. require "spec_helper" +require "ffi_yajl/ext/dlopen" describe "FFI_Yajl::Parser" do shared_examples_for "correct json parsing" do -- cgit v1.2.1 From f3af79db6d326b9d905865f220e5a36a1b4c3b05 Mon Sep 17 00:00:00 2001 From: skeshari12 Date: Tue, 12 Apr 2022 17:42:39 +0530 Subject: remove spec for jruby Signed-off-by: skeshari12 --- Rakefile | 7 ------- 1 file changed, 7 deletions(-) diff --git a/Rakefile b/Rakefile index ce105db..1444882 100644 --- a/Rakefile +++ b/Rakefile @@ -87,13 +87,6 @@ namespace :spec do ENV["FORCE_FFI_YAJL"] = "ffi" t.pattern = FileList["spec/**/*_spec.rb"] end - if !defined?(RUBY_ENGINE) || RUBY_ENGINE !~ /jruby/ - desc "Run all specs again c extension" - RSpec::Core::RakeTask.new(:ext) do |t| - ENV["FORCE_FFI_YAJL"] = "ext" - t.pattern = FileList["spec/**/*_spec.rb"] - end - end end namespace :integration do -- cgit v1.2.1 From 2988001d6db3078119346964bb1ccae895520bf4 Mon Sep 17 00:00:00 2001 From: skeshari12 Date: Tue, 12 Apr 2022 18:04:45 +0530 Subject: add again spec for jruby Signed-off-by: skeshari12 --- .expeditor/buildkite/run_windows_tests.ps1 | 6 +++++- Rakefile | 7 +++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/.expeditor/buildkite/run_windows_tests.ps1 b/.expeditor/buildkite/run_windows_tests.ps1 index 23f02c3..3e2277b 100644 --- a/.expeditor/buildkite/run_windows_tests.ps1 +++ b/.expeditor/buildkite/run_windows_tests.ps1 @@ -4,17 +4,21 @@ $ErrorActionPreference = "Stop" # This will run ruby test on windows platform echo "--- Install make " -choco install make --source=cygwin +choco install make --source=cygwin +If ($lastexitcode -ne 0) { Exit $lastexitcode } Write-Output "--- Bundle install" ruby --version bundler --version gem update --system +If ($lastexitcode -ne 0) { Exit $lastexitcode } bundle install --without development_extras --jobs 3 --retry 3 --path vendor/bundle +If ($lastexitcode -ne 0) { Exit $lastexitcode } gem install yajl-ruby json psych If ($lastexitcode -ne 0) { Exit $lastexitcode } Write-Output "--- Bundle Execute" bundle exec rake compile +If ($lastexitcode -ne 0) { Exit $lastexitcode } bundle exec rake spec If ($lastexitcode -ne 0) { Exit $lastexitcode } \ No newline at end of file diff --git a/Rakefile b/Rakefile index 1444882..ce105db 100644 --- a/Rakefile +++ b/Rakefile @@ -87,6 +87,13 @@ namespace :spec do ENV["FORCE_FFI_YAJL"] = "ffi" t.pattern = FileList["spec/**/*_spec.rb"] end + if !defined?(RUBY_ENGINE) || RUBY_ENGINE !~ /jruby/ + desc "Run all specs again c extension" + RSpec::Core::RakeTask.new(:ext) do |t| + ENV["FORCE_FFI_YAJL"] = "ext" + t.pattern = FileList["spec/**/*_spec.rb"] + end + end end namespace :integration do -- cgit v1.2.1 From 38542f10ada8ec5c3a264549382ef75c813ec672 Mon Sep 17 00:00:00 2001 From: skeshari12 Date: Tue, 12 Apr 2022 18:11:36 +0530 Subject: fix error Signed-off-by: skeshari12 --- spec/ffi_yajl/encoder_spec.rb | 1 - spec/ffi_yajl/map_library_name_spec.rb | 1 - spec/ffi_yajl/parser_spec.rb | 1 - 3 files changed, 3 deletions(-) diff --git a/spec/ffi_yajl/encoder_spec.rb b/spec/ffi_yajl/encoder_spec.rb index d2612a5..7a76c0a 100644 --- a/spec/ffi_yajl/encoder_spec.rb +++ b/spec/ffi_yajl/encoder_spec.rb @@ -23,7 +23,6 @@ require "spec_helper" require "date" -require "ffi_yajl/ext/dlopen" describe "FFI_Yajl::Encoder" do let(:options) { {} } diff --git a/spec/ffi_yajl/map_library_name_spec.rb b/spec/ffi_yajl/map_library_name_spec.rb index e63fadc..6ec8bb6 100644 --- a/spec/ffi_yajl/map_library_name_spec.rb +++ b/spec/ffi_yajl/map_library_name_spec.rb @@ -21,7 +21,6 @@ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. require "spec_helper" -require "ffi_yajl/ext/dlopen" class Test extend FFI_Yajl::MapLibraryName diff --git a/spec/ffi_yajl/parser_spec.rb b/spec/ffi_yajl/parser_spec.rb index 9ac924a..239ae26 100644 --- a/spec/ffi_yajl/parser_spec.rb +++ b/spec/ffi_yajl/parser_spec.rb @@ -22,7 +22,6 @@ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. require "spec_helper" -require "ffi_yajl/ext/dlopen" describe "FFI_Yajl::Parser" do shared_examples_for "correct json parsing" do -- cgit v1.2.1 From 164fca12782575349e76b024d1c880df57de1e3f Mon Sep 17 00:00:00 2001 From: "Marc A. Paradise" Date: Tue, 12 Apr 2022 11:58:45 -0400 Subject: Use build tools that are included with devkit These are in the devkit that is part of the base image and are guaranteed correct for native compilation of gems against these versions of ruby. Signed-off-by: Marc A. Paradise --- .expeditor/buildkite/run_windows_tests.ps1 | 44 +++++++++++++++++++++--------- .expeditor/verify.pipeline.yml | 4 +-- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/.expeditor/buildkite/run_windows_tests.ps1 b/.expeditor/buildkite/run_windows_tests.ps1 index 3e2277b..ed5c28a 100644 --- a/.expeditor/buildkite/run_windows_tests.ps1 +++ b/.expeditor/buildkite/run_windows_tests.ps1 @@ -1,24 +1,42 @@ -# Stop script execution when a non-terminating error occurs -$ErrorActionPreference = "Stop" +param([String]$version) +# This script will run ruby test on windows platform. It requires a version +# "3.0" or "3.1" as an argument. -# This will run ruby test on windows platform +# Stop script execution when a non-terminating error occurs. Note that this makes it +# unneccesary to check the exit code of each program being run - non-zero exit will force it to fail and terminate. +$ErrorActionPreference = "Stop" -echo "--- Install make " -choco install make --source=cygwin -If ($lastexitcode -ne 0) { Exit $lastexitcode } +# The specific paths of tools within the ruby30/31 devkit vary a bit across 3.0 and 3.1 +if ($version -eq "3.0") +{ + $base_dir = "C:\ruby30\" + $Env:PATH += ";" + $base_dir + "ruby\bin;" + $base_dir + "msys64\usr\bin;" + $base_dir + "msys64\mingw64\bin" +} +elseif($version -eq "3.1") +{ + $base_dir = "C:\ruby31\" + # Note path change - gcc is living in ucrt64\bin here, and mingw64 in earlier versions. + $Env:PATH += ";" + $base_dir + "ruby\bin;" + $base_dir + "msys64\usr\bin;" + $base_dir + "msys64\ucrt64\bin" +} -Write-Output "--- Bundle install" +Write-Output "--- Ensuring required bins are in path" +Write-Output "PATH: " + $Env:PATH +make --version +gcc --version ruby --version bundler --version + +Write-Output "--- Updating system gems" gem update --system -If ($lastexitcode -ne 0) { Exit $lastexitcode } + +Write-Output "--- Bundle install" bundle install --without development_extras --jobs 3 --retry 3 --path vendor/bundle -If ($lastexitcode -ne 0) { Exit $lastexitcode } + +Write-Output "--- Gem install" gem install yajl-ruby json psych -If ($lastexitcode -ne 0) { Exit $lastexitcode } -Write-Output "--- Bundle Execute" +Write-Output "--- Bundle Execute: rake compile" bundle exec rake compile -If ($lastexitcode -ne 0) { Exit $lastexitcode } + +Write-Output "--- Bundle Execute: rake spec" bundle exec rake spec -If ($lastexitcode -ne 0) { Exit $lastexitcode } \ No newline at end of file diff --git a/.expeditor/verify.pipeline.yml b/.expeditor/verify.pipeline.yml index 83ce300..c105118 100644 --- a/.expeditor/verify.pipeline.yml +++ b/.expeditor/verify.pipeline.yml @@ -61,7 +61,7 @@ steps: - label: "run-specs-ruby-3.0-windows" command: - - .expeditor/buildkite/run_windows_tests.ps1 + - .expeditor/buildkite/run_windows_tests.ps1 "3.0" timeout_in_minutes: 20 expeditor: executor: @@ -75,7 +75,7 @@ steps: - label: "run-specs-ruby-3.1-windows" command: - - .expeditor/buildkite/run_windows_tests.ps1 + - .expeditor/buildkite/run_windows_tests.ps1 "3.1" timeout_in_minutes: 20 expeditor: executor: -- cgit v1.2.1 From 511ffa94c171fb96d0c232002c2bb67a155d4c67 Mon Sep 17 00:00:00 2001 From: "Marc A. Paradise" Date: Wed, 13 Apr 2022 11:26:23 -0400 Subject: make test names consistent Signed-off-by: Marc A. Paradise --- .expeditor/verify.pipeline.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.expeditor/verify.pipeline.yml b/.expeditor/verify.pipeline.yml index c105118..10b5e42 100644 --- a/.expeditor/verify.pipeline.yml +++ b/.expeditor/verify.pipeline.yml @@ -59,7 +59,7 @@ steps: - BUNDLE_GEMFILE=/workdir/Gemfile - FORCE_FFI_YAJL="ext" -- label: "run-specs-ruby-3.0-windows" +- label: ":windows: :ruby: 3.0" command: - .expeditor/buildkite/run_windows_tests.ps1 "3.0" timeout_in_minutes: 20 @@ -73,7 +73,7 @@ steps: - BUNDLE_GEMFILE=/workdir/Gemfile - FORCE_FFI_YAJL="ext" -- label: "run-specs-ruby-3.1-windows" +- label: ":windows: :ruby: 3.1" command: - .expeditor/buildkite/run_windows_tests.ps1 "3.1" timeout_in_minutes: 20 -- cgit v1.2.1