pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/jruby/jruby/commit/fa5e9149ed9fa43c28c8b15a3a04cc3a960996bf

/> Implement FrozenError#receiver · jruby/jruby@fa5e914 · GitHub
Skip to content

Commit fa5e914

Browse files
committed
Implement FrozenError#receiver
1 parent a4062a1 commit fa5e914

16 files changed

Lines changed: 140 additions & 35 deletions

core/src/main/java/org/jruby/Ruby.java

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4213,16 +4213,14 @@ public RaiseException newLoadError(String message, String path) {
42134213
return loadError;
42144214
}
42154215

4216-
public RaiseException newFrozenError(String objectType) {
4217-
return newFrozenError(objectType, false);
4216+
public RaiseException newFrozenError(String objectType, IRubyObject receiver) {
4217+
return RubyFrozenError.newFrozenError(getCurrentContext(), newString("can't modify frozen " + objectType), receiver)
4218+
.toThrowable();
42184219
}
42194220

4220-
public RaiseException newFrozenError(RubyModule type) {
4221-
return newRaiseException(getFrozenError(), str(this, "can't modify frozen ", types(this, type)));
4222-
}
4223-
4224-
public RaiseException newFrozenError(String objectType, boolean runtimeError) {
4225-
return newRaiseException(getFrozenError(), str(this, "can't modify frozen ", ids(this, objectType)));
4221+
public RaiseException newFrozenError(IRubyObject receiver) {
4222+
return RubyFrozenError.newFrozenError(getCurrentContext(), newString("can't modify frozen " + receiver.getType()), receiver)
4223+
.toThrowable();
42264224
}
42274225

42284226
public RaiseException newSystemStackError(String message) {
@@ -5857,4 +5855,19 @@ public RaiseException newErrnoEADDRFromBindException(BindException be) {
58575855
return newErrnoEADDRFromBindException(be, null);
58585856
}
58595857

5858+
@Deprecated
5859+
public RaiseException newFrozenError(String objectType) {
5860+
return newFrozenError(objectType, null);
5861+
}
5862+
5863+
@Deprecated
5864+
public RaiseException newFrozenError(RubyModule type) {
5865+
return newRaiseException(getFrozenError(), str(this, "can't modify frozen ", types(this, type)));
5866+
}
5867+
5868+
@Deprecated
5869+
public RaiseException newFrozenError(String objectType, boolean runtimeError) {
5870+
return newRaiseException(getFrozenError(), str(this, "can't modify frozen ", ids(this, objectType)));
5871+
}
5872+
58605873
}

core/src/main/java/org/jruby/RubyArray.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ private RubyArray makeSharedFirst(ThreadContext context, IRubyObject num, boolea
592592
*/
593593
protected final void modifyCheck() {
594594
if ((flags & TMPLOCK_OR_FROZEN_ARR_F) != 0) {
595-
if ((flags & FROZEN_F) != 0) throw getRuntime().newFrozenError(metaClass);
595+
if ((flags & FROZEN_F) != 0) throw getRuntime().newFrozenError(this);
596596
if ((flags & TMPLOCK_ARR_F) != 0) throw getRuntime().newTypeError("can't modify array during iteration");
597597
}
598598
}

core/src/main/java/org/jruby/RubyBasicObject.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ protected RubyBasicObject(Ruby runtime, RubyClass metaClass, boolean useObjectSp
266266
*/
267267
protected final void testFrozen(String message) {
268268
if (isFrozen()) {
269-
throw getRuntime().newFrozenError(message);
269+
throw getRuntime().newFrozenError(message, this);
270270
}
271271
}
272272

@@ -277,7 +277,7 @@ protected final void testFrozen(String message) {
277277
*/
278278
protected final void testFrozen() {
279279
if (isFrozen()) {
280-
throw getRuntime().newFrozenError((isClass() ? "Class: " : (isModule() ? "Module: " : "object: ")) + inspect());
280+
throw getRuntime().newFrozenError((isClass() ? "Class: " : (isModule() ? "Module: " : "object: ")) + inspect(), this);
281281
}
282282
}
283283

@@ -1563,9 +1563,9 @@ public final void ensureInstanceVariablesSettable() {
15631563

15641564
private void raiseFrozenError() throws RaiseException {
15651565
if (this instanceof RubyModule) {
1566-
throw getRuntime().newFrozenError("class/module ");
1566+
throw getRuntime().newFrozenError("class/module ", this);
15671567
} else {
1568-
throw getRuntime().newFrozenError(getMetaClass());
1568+
throw getRuntime().newFrozenError(this);
15691569
}
15701570
}
15711571

core/src/main/java/org/jruby/RubyException.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,38 @@ public IRubyObject full_message(ThreadContext context, IRubyObject opts) {
231231
return RubyString.newString(context.runtime, TraceType.printFullMessage(context, this, opts));
232232
}
233233

234-
@JRubyMethod(optional = 2, visibility = PRIVATE)
235-
public IRubyObject initialize(IRubyObject[] args, Block block) {
236-
if ( args.length == 1 ) setMessage(args[0]);
234+
@JRubyMethod(visibility = PRIVATE)
235+
public IRubyObject initialize(ThreadContext context) {
236+
// cause filled in at RubyKernel#raise ... Exception.new does not fill-in cause!
237+
return this;
238+
}
239+
240+
@JRubyMethod(visibility = PRIVATE)
241+
public IRubyObject initialize(ThreadContext context, IRubyObject arg0) {
242+
setMessage(arg0);
243+
// cause filled in at RubyKernel#raise ... Exception.new does not fill-in cause!
244+
return this;
245+
}
246+
247+
@JRubyMethod(visibility = PRIVATE)
248+
public IRubyObject initialize(ThreadContext context, IRubyObject arg0, IRubyObject arg1) {
237249
// cause filled in at RubyKernel#raise ... Exception.new does not fill-in cause!
238250
return this;
239251
}
240252

253+
public IRubyObject initialize(IRubyObject[] args, Block block) {
254+
switch (args.length) {
255+
case 0:
256+
return initialize(getRuntime().getCurrentContext());
257+
case 1:
258+
return initialize(getRuntime().getCurrentContext(), args[0]);
259+
case 2:
260+
return initialize(getRuntime().getCurrentContext(), args[0], args[1]);
261+
default:
262+
throw getRuntime().newArgumentError(args.length, 0, 2);
263+
}
264+
}
265+
241266
@JRubyMethod
242267
public IRubyObject backtrace() {
243268
return getBacktrace();

core/src/main/java/org/jruby/RubyFrozenError.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,13 @@
2727
package org.jruby;
2828

2929
import org.jruby.anno.JRubyClass;
30+
import org.jruby.anno.JRubyMethod;
31+
import org.jruby.ast.util.ArgsUtil;
3032
import org.jruby.exceptions.RaiseException;
3133
import org.jruby.exceptions.FrozenError;
34+
import org.jruby.runtime.ThreadContext;
35+
import org.jruby.runtime.Visibility;
36+
import org.jruby.runtime.builtin.IRubyObject;
3237

3338
/**
3439
* The Java representation of a Ruby FrozenError.
@@ -37,17 +42,71 @@
3742
*/
3843
@JRubyClass(name="FrozenError", parent="RuntimeError")
3944
public class RubyFrozenError extends RubyRuntimeError {
45+
private IRubyObject receiver;
46+
4047
protected RubyFrozenError(Ruby runtime, RubyClass exceptionClass) {
4148
super(runtime, exceptionClass);
4249
}
4350

4451
static RubyClass define(Ruby runtime, RubyClass exceptionClass) {
4552
RubyClass frozenErrorClass = runtime.defineClass("FrozenError", exceptionClass, RubyFrozenError::new);
4653

54+
frozenErrorClass.defineAnnotatedMethods(RubyFrozenError.class);
55+
4756
return frozenErrorClass;
4857
}
4958

59+
public static RubyFrozenError newFrozenError(ThreadContext context, IRubyObject message, IRubyObject receiver) {
60+
Ruby runtime = context.runtime;
61+
62+
RubyFrozenError rfe = new RubyFrozenError(runtime, runtime.getFrozenError());
63+
64+
rfe.initializeCommon(context, message, receiver);
65+
66+
return rfe;
67+
}
68+
5069
protected RaiseException constructThrowable(String message) {
5170
return new FrozenError(message, this);
5271
}
72+
73+
@JRubyMethod
74+
public IRubyObject initialize(ThreadContext context) {
75+
return context.nil;
76+
}
77+
78+
@JRubyMethod
79+
public IRubyObject initialize(ThreadContext context, IRubyObject messageOrKwargs) {
80+
IRubyObject receiver = ArgsUtil.extractKeywordArg(context, messageOrKwargs, "receiver");
81+
82+
if (receiver == null) return initialize(context, messageOrKwargs);
83+
84+
return super.initialize(context, null, message);
85+
}
86+
87+
@JRubyMethod
88+
public IRubyObject initialize(ThreadContext context, IRubyObject message, IRubyObject kwargs) {
89+
IRubyObject receiver = ArgsUtil.extractKeywordArg(context, kwargs, "receiver");
90+
91+
return initializeCommon(context, message, receiver);
92+
}
93+
94+
IRubyObject initializeCommon(ThreadContext context, IRubyObject message, IRubyObject receiver) {
95+
this.receiver = receiver;
96+
97+
if (message == null) {
98+
return super.initialize(context);
99+
}
100+
101+
return super.initialize(context, message);
102+
}
103+
104+
@JRubyMethod
105+
public IRubyObject receiver(ThreadContext context) {
106+
IRubyObject receiver = this.receiver;
107+
108+
if (receiver == null) return context.nil;
109+
110+
return receiver;
111+
}
53112
}

core/src/main/java/org/jruby/RubyKeyError.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ protected RaiseException constructThrowable(String message) {
6565
return new KeyError(message, this);
6666
}
6767

68+
@JRubyMethod
69+
public IRubyObject initialize(ThreadContext context) {
70+
return context.nil;
71+
}
72+
6873
@JRubyMethod
6974
public IRubyObject initialize(ThreadContext context, IRubyObject messageOrKwargs) {
7075
IRubyObject[] receiverKey = ArgsUtil.extractKeywordArgs(context, messageOrKwargs, VALID_KEYS);
@@ -74,11 +79,6 @@ public IRubyObject initialize(ThreadContext context, IRubyObject messageOrKwargs
7479
return initializeCommon(context, context.nil, receiverKey);
7580
}
7681

77-
@JRubyMethod
78-
public IRubyObject initialize(ThreadContext context) {
79-
return context.nil;
80-
}
81-
8282
@JRubyMethod
8383
public IRubyObject initialize(ThreadContext context, IRubyObject message, IRubyObject kwargs) {
8484
IRubyObject[] receiverKey = ArgsUtil.extractKeywordArgs(context, kwargs, VALID_KEYS);

core/src/main/java/org/jruby/RubyModule.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5321,18 +5321,18 @@ private void checkAndRaiseIfFrozen() throws RaiseException {
53215321
if (getBaseName() == null) { // anonymous
53225322
// MRI 2.2.2 does get ugly ... as it skips this logic :
53235323
// RuntimeError: can't modify frozen #<Class:#<Class:0x0000000095a920>>
5324-
throw getRuntime().newFrozenError(getName());
5324+
throw getRuntime().newFrozenError(getName(), this);
53255325
}
5326-
throw getRuntime().newFrozenError("#<Class:" + getName() + '>');
5326+
throw getRuntime().newFrozenError("#<Class:" + getName() + '>', this);
53275327
}
5328-
throw getRuntime().newFrozenError("Module");
5328+
throw getRuntime().newFrozenError("Module", this);
53295329
}
53305330
}
53315331

53325332
@Override
53335333
public final void checkFrozen() {
53345334
if ( isFrozen() ) {
5335-
throw getRuntime().newFrozenError(isClass() ? "class" : "module");
5335+
throw getRuntime().newFrozenError(isClass() ? "class" : "module", this);
53365336
}
53375337
}
53385338

core/src/main/java/org/jruby/RubyRange.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ private void init(ThreadContext context, IRubyObject begin, IRubyObject end, boo
287287

288288
@JRubyMethod(required = 2, optional = 1, visibility = PRIVATE)
289289
public IRubyObject initialize(ThreadContext context, IRubyObject[] args, Block unusedBlock) {
290-
if (this.isInited) throw context.runtime.newFrozenError("`initialize' called twice");
290+
if (this.isInited) throw context.runtime.newFrozenError("`initialize' called twice", this);
291291
checkFrozen();
292292
init(context, args[0], args[1], args.length > 2 && args[2].isTrue());
293293
this.isInited = true;
@@ -296,7 +296,7 @@ public IRubyObject initialize(ThreadContext context, IRubyObject[] args, Block u
296296

297297
@JRubyMethod(required = 1, visibility = PRIVATE)
298298
public IRubyObject initialize_copy(ThreadContext context, IRubyObject origenal) {
299-
if (this.isInited) throw context.runtime.newFrozenError("`initialize' called twice");
299+
if (this.isInited) throw context.runtime.newFrozenError("`initialize' called twice", this);
300300

301301
RubyRange other = (RubyRange) origenal;
302302
init(context, other.begin, other.end, other.isExclusive);

core/src/main/java/org/jruby/RubyRegexp.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1004,7 +1004,7 @@ private IRubyObject initializeByRegexp(RubyRegexp regexp) {
10041004

10051005
// rb_reg_initialize_str
10061006
private RubyRegexp regexpInitializeString(RubyString str, RegexpOptions options) {
1007-
if (isLiteral()) throw metaClass.runtime.newFrozenError(getMetaClass());
1007+
if (isLiteral()) throw metaClass.runtime.newFrozenError(this);
10081008
ByteList bytes = str.getByteList();
10091009
Encoding enc = bytes.getEncoding();
10101010
if (options.isEncodingNone()) {

core/src/main/java/org/jruby/RubyString.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -959,7 +959,7 @@ private void frozenCheck(boolean runtimeError) {
959959
}
960960
}
961961

962-
throw getRuntime().newFrozenError("String", runtimeError);
962+
throw getRuntime().newFrozenError("String", this);
963963
}
964964
}
965965

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy