comparison graal/com.oracle.truffle.ruby.nodes/src/com/oracle/truffle/ruby/nodes/core/ObjectSpaceNodes.java @ 13514:0fbee3eb71f0

Ruby: import project.
author Chris Seaton <chris.seaton@oracle.com>
date Mon, 06 Jan 2014 17:12:09 +0000
parents
children
comparison
equal deleted inserted replaced
13513:64a23ce736a0 13514:0fbee3eb71f0
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.core;
11
12 import java.math.*;
13
14 import com.oracle.truffle.api.*;
15 import com.oracle.truffle.api.dsl.*;
16 import com.oracle.truffle.api.frame.*;
17 import com.oracle.truffle.ruby.runtime.*;
18 import com.oracle.truffle.ruby.runtime.core.*;
19 import com.oracle.truffle.ruby.runtime.objects.*;
20
21 @CoreClass(name = "ObjectSpace")
22 public abstract class ObjectSpaceNodes {
23
24 @CoreMethod(names = "_id2ref", isModuleMethod = true, needsSelf = false, minArgs = 1, maxArgs = 1)
25 public abstract static class ID2RefNode extends CoreMethodNode {
26
27 public ID2RefNode(RubyContext context, SourceSection sourceSection) {
28 super(context, sourceSection);
29 }
30
31 public ID2RefNode(ID2RefNode prev) {
32 super(prev);
33 }
34
35 @Specialization
36 public Object id2Ref(int id) {
37 final Object object = getContext().getObjectSpaceManager().lookupId(id);
38
39 if (object == null) {
40 return NilPlaceholder.INSTANCE;
41 } else {
42 return object;
43 }
44 }
45
46 @Specialization
47 public Object id2Ref(BigInteger id) {
48 final Object object = getContext().getObjectSpaceManager().lookupId(id.longValue());
49
50 if (object == null) {
51 return NilPlaceholder.INSTANCE;
52 } else {
53 return object;
54 }
55 }
56
57 }
58
59 @CoreMethod(names = "each_object", isModuleMethod = true, needsSelf = false, needsBlock = true, minArgs = 0, maxArgs = 1)
60 public abstract static class EachObjectNode extends YieldingCoreMethodNode {
61
62 public EachObjectNode(RubyContext context, SourceSection sourceSection) {
63 super(context, sourceSection);
64 }
65
66 public EachObjectNode(EachObjectNode prev) {
67 super(prev);
68 }
69
70 @Specialization
71 public NilPlaceholder eachObject(VirtualFrame frame, @SuppressWarnings("unused") UndefinedPlaceholder ofClass, RubyProc block) {
72 for (RubyBasicObject object : getContext().getObjectSpaceManager().getObjects()) {
73 yield(frame, block, object);
74 }
75 return NilPlaceholder.INSTANCE;
76 }
77
78 @Specialization
79 public NilPlaceholder eachObject(VirtualFrame frame, RubyClass ofClass, RubyProc block) {
80 for (RubyBasicObject object : getContext().getObjectSpaceManager().getObjects()) {
81 if (object.getRubyClass().assignableTo(ofClass)) {
82 yield(frame, block, object);
83 }
84 }
85 return NilPlaceholder.INSTANCE;
86 }
87
88 }
89
90 @CoreMethod(names = "define_finalizer", isModuleMethod = true, needsSelf = false, minArgs = 2, maxArgs = 2)
91 public abstract static class DefineFinalizerNode extends CoreMethodNode {
92
93 public DefineFinalizerNode(RubyContext context, SourceSection sourceSection) {
94 super(context, sourceSection);
95 }
96
97 public DefineFinalizerNode(DefineFinalizerNode prev) {
98 super(prev);
99 }
100
101 @Specialization
102 public RubyProc defineFinalizer(Object object, RubyProc finalizer) {
103 getContext().getObjectSpaceManager().defineFinalizer((RubyBasicObject) object, finalizer);
104 return finalizer;
105 }
106 }
107
108 @CoreMethod(names = {"garbage_collect", "start"}, isModuleMethod = true, needsSelf = false, maxArgs = 0)
109 public abstract static class GarbageCollectNode extends CoreMethodNode {
110
111 public GarbageCollectNode(RubyContext context, SourceSection sourceSection) {
112 super(context, sourceSection);
113 }
114
115 public GarbageCollectNode(GarbageCollectNode prev) {
116 super(prev);
117 }
118
119 @Specialization
120 public NilPlaceholder garbageCollect() {
121 final RubyThread runningThread = getContext().getThreadManager().leaveGlobalLock();
122
123 try {
124 System.gc();
125 } finally {
126 getContext().getThreadManager().enterGlobalLock(runningThread);
127 }
128
129 return NilPlaceholder.INSTANCE;
130 }
131 }
132
133 @CoreMethod(names = "undefine_finalizer", isModuleMethod = true, needsSelf = false, minArgs = 1, maxArgs = 1)
134 public abstract static class UndefineFinalizerNode extends CoreMethodNode {
135
136 public UndefineFinalizerNode(RubyContext context, SourceSection sourceSection) {
137 super(context, sourceSection);
138 }
139
140 public UndefineFinalizerNode(UndefineFinalizerNode prev) {
141 super(prev);
142 }
143
144 @Specialization
145 public Object undefineFinalizer(Object object) {
146 getContext().getObjectSpaceManager().undefineFinalizer((RubyBasicObject) object);
147 return object;
148 }
149 }
150
151 }