Skip to content

Commit 96f2b2a

Browse files
committed
[GR-19220] Remove the NEWOBJ and OBJSETUP macros (#2871)
PullRequest: truffleruby/3657
2 parents 907adf6 + 5ad03a6 commit 96f2b2a

File tree

7 files changed

+121
-1
lines changed

7 files changed

+121
-1
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ jobs:
1212
steps:
1313
- name: Clone TruffleRuby
1414
uses: actions/checkout@v2
15+
with:
16+
fetch-depth: 0 # Necessary for jt check_abi
1517
- name: Setup system Ruby
1618
uses: ruby/setup-ruby@v1
1719
- name: Setup jt

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ Bug fixes:
239239
* Fix `Enumerator::Lazy#with_index` to start with new index for multiple enumerations (@bjfish).
240240
* Fix `rb_id2name` to ensure the native string will have the same lifetime as the id (#2630, @aardvark179).
241241
* Fix `Integer#fdiv` and `Rational#to_f` for large `Integer` values (#2631, @bjfish).
242+
* Remove the `RB_NEWOBJ/NEWOBJ` and `OBJSETUP` macros since we cannot support them in TruffleRuby and native extensions may use `#ifdef` to detect features (#2869, @nirvdrum).
242243

243244
Compatibility:
244245

lib/cext/ABI_version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1
1+
2

lib/cext/include/ruby/internal/newobj.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@
4242
*
4343
* :FIXME: Should we deprecate it?
4444
*/
45+
// We cannot support RB_NEWOBJ/NEWOBJ and OBJSETUP in TruffleRuby. They rely on being able to create an object
46+
// without an associated class and updating state that is tied directly to MRI's object layout.
47+
#ifndef TRUFFLERUBY
4548
#define RB_NEWOBJ(obj,type) type *(obj) = RBIMPL_CAST((type *)rb_newobj())
49+
#endif
4650

4751
/**
4852
* Identical to #RB_NEWOBJ, except it also accepts the allocating object's
@@ -57,9 +61,13 @@
5761
*/
5862
#define RB_NEWOBJ_OF(obj,type,klass,flags) type *(obj) = RBIMPL_CAST((type *)rb_newobj_of(klass, flags))
5963

64+
#ifndef TRUFFLERUBY
6065
#define NEWOBJ RB_NEWOBJ /**< @old{RB_NEWOBJ} */
66+
#endif
6167
#define NEWOBJ_OF RB_NEWOBJ_OF /**< @old{RB_NEWOBJ_OF} */
68+
#ifndef TRUFFLERUBY
6269
#define OBJSETUP rb_obj_setup /**< @old{rb_obj_setup} */
70+
#endif
6371
#define CLONESETUP rb_clone_setup /**< @old{rb_clone_setup} */
6472
#define DUPSETUP rb_dup_setup /**< @old{rb_dup_setup} */
6573

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved. This
3+
* code is released under a tri EPL/GPL/LGPL license. You can use it,
4+
* redistribute it and/or modify it under the terms of the:
5+
*
6+
* Eclipse Public License version 2.0, or
7+
* GNU General Public License version 2, or
8+
* GNU Lesser General Public License version 2.1.
9+
*/
10+
#include "ruby.h"
11+
#include "rubyspec.h"
12+
13+
#include <stdio.h>
14+
15+
#ifdef __cplusplus
16+
extern "C" {
17+
#endif
18+
19+
static VALUE newobj_RB_NEWOBJ_defined(VALUE self) {
20+
#ifdef RB_NEWOBJ
21+
return Qtrue;
22+
#else
23+
return Qfalse;
24+
#endif
25+
}
26+
27+
static VALUE newobj_NEWOBJ_defined(VALUE self) {
28+
#ifdef NEWOBJ
29+
return Qtrue;
30+
#else
31+
return Qfalse;
32+
#endif
33+
}
34+
35+
static VALUE newobj_OBJSETUP_defined(VALUE self) {
36+
#ifdef OBJSETUP
37+
return Qtrue;
38+
#else
39+
return Qfalse;
40+
#endif
41+
}
42+
43+
static VALUE newobj_NEWOBJ_OF_defined(VALUE self) {
44+
#ifdef NEWOBJ_OF
45+
return Qtrue;
46+
#else
47+
return Qfalse;
48+
#endif
49+
}
50+
51+
static VALUE newobj_RB_NEWOBJ_OF_defined(VALUE self) {
52+
#ifdef RB_NEWOBJ_OF
53+
return Qtrue;
54+
#else
55+
return Qfalse;
56+
#endif
57+
}
58+
59+
void Init_truffleruby_newobj_spec(void) {
60+
VALUE cls;
61+
cls = rb_define_class("CApiTruffleRubyNewobjSpecs", rb_cObject);
62+
rb_define_method(cls, "RB_NEWOBJ_defined?", newobj_RB_NEWOBJ_defined, 0);
63+
rb_define_method(cls, "NEWOBJ_defined?", newobj_NEWOBJ_defined, 0);
64+
rb_define_method(cls, "NEWOBJ_OF_defined?", newobj_NEWOBJ_OF_defined, 0);
65+
rb_define_method(cls, "RB_NEWOBJ_OF_defined?", newobj_RB_NEWOBJ_OF_defined, 0);
66+
rb_define_method(cls, "OBJSETUP_defined?", newobj_OBJSETUP_defined, 0);
67+
}
68+
69+
#ifdef __cplusplus
70+
}
71+
#endif

spec/truffle/capi/newobj_spec.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved. This
2+
# code is released under a tri EPL/GPL/LGPL license. You can use it,
3+
# redistribute it and/or modify it under the terms of the:
4+
#
5+
# Eclipse Public License version 2.0, or
6+
# GNU General Public License version 2, or
7+
# GNU Lesser General Public License version 2.1.
8+
9+
require_relative '../../ruby/optional/capi/spec_helper'
10+
11+
load_extension("truffleruby_newobj")
12+
13+
describe "TruffleRuby handling of the NEWOBJ API" do
14+
before :each do
15+
@s = CApiTruffleRubyNewobjSpecs.new
16+
end
17+
18+
it "does not support NEWOBJ since we cannot create an object without a specified class" do
19+
@s.RB_NEWOBJ_defined?.should be_false
20+
end
21+
22+
it "does not support NEWOBJ since we cannot create an object without a specified class" do
23+
@s.NEWOBJ_defined?.should be_false
24+
end
25+
26+
it "does not support OBJSETUP since we use a different object layout than MRI" do
27+
@s.OBJSETUP_defined?.should be_false
28+
end
29+
30+
it "does support NEWOBJ_OF since we can create an object with a specified class" do
31+
@s.NEWOBJ_OF_defined?.should be_true
32+
end
33+
34+
it "does support RB_NEWOBJ_OF since we can create an object with a specified class" do
35+
@s.RB_NEWOBJ_OF_defined?.should be_true
36+
end
37+
end

tool/jt.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3053,6 +3053,7 @@ def lint(*args)
30533053
ABI_CHECK_FILE = 'lib/cext/ABI_check.txt'
30543054

30553055
def check_abi(fail: true)
3056+
puts bold '$ jt check_abi'
30563057
# Check since the last commit at which ABI_CHECK_FILE or ABI_VERSION_FILE were modified
30573058
base_commit = `git log -n 1 --format=%H #{ABI_VERSION_FILE} #{ABI_CHECK_FILE}`.chomp
30583059

0 commit comments

Comments
 (0)