13514
|
1 /*
|
|
2 * Copyright (c) 2013 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 1.0
|
|
7 * GNU General Public License version 2
|
|
8 * GNU Lesser General Public License version 2.1
|
|
9 */
|
|
10 package com.oracle.truffle.ruby.nodes.cast;
|
|
11
|
|
12 import com.oracle.truffle.api.*;
|
|
13 import com.oracle.truffle.api.dsl.*;
|
|
14 import com.oracle.truffle.api.frame.*;
|
|
15 import com.oracle.truffle.api.nodes.*;
|
|
16 import com.oracle.truffle.ruby.nodes.*;
|
|
17 import com.oracle.truffle.ruby.runtime.*;
|
|
18 import com.oracle.truffle.ruby.runtime.core.*;
|
|
19
|
|
20 /**
|
|
21 * Casts a value into a boolean. Works at the language level, so doesn't call any Ruby methods to
|
|
22 * cast non-core or monkey-patched objects.
|
|
23 */
|
|
24 @NodeInfo(shortName = "cast-boolean")
|
|
25 @NodeChild(value = "child", type = RubyNode.class)
|
|
26 public abstract class BooleanCastNode extends RubyNode {
|
|
27
|
|
28 public BooleanCastNode(RubyContext context, SourceSection sourceSection) {
|
|
29 super(context, sourceSection);
|
|
30 }
|
|
31
|
|
32 public BooleanCastNode(BooleanCastNode copy) {
|
|
33 super(copy.getContext(), copy.getSourceSection());
|
|
34 }
|
|
35
|
|
36 @Specialization
|
|
37 public boolean doBoolean(boolean value) {
|
|
38 return value;
|
|
39 }
|
|
40
|
|
41 @Specialization
|
|
42 public boolean doNil(@SuppressWarnings("unused") NilPlaceholder nil) {
|
|
43 return false;
|
|
44 }
|
|
45
|
|
46 @Generic
|
|
47 public boolean doGeneric(Object object) {
|
|
48 if (object instanceof Boolean) {
|
|
49 return (boolean) object;
|
|
50 } else if (object instanceof NilPlaceholder || object instanceof RubyNilClass) {
|
|
51 return false;
|
|
52 } else {
|
|
53 return true;
|
|
54 }
|
|
55 }
|
|
56
|
|
57 @Override
|
|
58 public abstract boolean executeBoolean(VirtualFrame frame);
|
|
59
|
|
60 }
|