Skip to content

Commit

Permalink
Showing 9 changed files with 180 additions and 2 deletions.
1 change: 1 addition & 0 deletions spec/truffle/tags/library/weakref/send_tags.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fails:WeakRef#__send__ delegates to protected methods of the weakly-referenced object
1 change: 0 additions & 1 deletion spec/truffle/truffle.mspec
Original file line number Diff line number Diff line change
@@ -84,7 +84,6 @@ class MSpecScript
"^spec/ruby/library/mathn",
"^spec/ruby/library/readline",
"^spec/ruby/library/syslog",
"^spec/ruby/library/weakref",

# Doesn't exist as Ruby code - basically need to write from scratch
"^spec/ruby/library/win32ole",
Original file line number Diff line number Diff line change
@@ -69,6 +69,7 @@ public void addAnnotatedPrimitives() {
nodeFactories.addAll(IOBufferPrimitiveNodesFactory.getFactories());
nodeFactories.addAll(ExceptionPrimitiveNodesFactory.getFactories());
nodeFactories.addAll(ThreadPrimitiveNodesFactory.getFactories());
nodeFactories.addAll(WeakRefPrimitiveNodesFactory.getFactories());

// This comes last as a catch-all
nodeFactories.addAll(UndefinedPrimitiveNodesFactory.getFactories());
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (c) 2015 Oracle and/or its affiliates. All rights reserved. This
* code is released under a tri EPL/GPL/LGPL license. You can use it,
* redistribute it and/or modify it under the terms of the:
*
* Eclipse Public License version 1.0
* GNU General Public License version 2
* GNU Lesser General Public License version 2.1
*/
package org.jruby.truffle.nodes.rubinius;

import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.object.DynamicObject;
import com.oracle.truffle.api.source.SourceSection;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.layouts.Layouts;

import java.lang.ref.WeakReference;

public abstract class WeakRefPrimitiveNodes {

@RubiniusPrimitive(name = "weakref_new", needsSelf = false)
public static abstract class WeakRefNewPrimitiveNode extends RubiniusPrimitiveNode {

public WeakRefNewPrimitiveNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

@Specialization
public DynamicObject weakRefNew(Object object) {
return Layouts.WEAK_REF_LAYOUT.createWeakRef(getContext().getCoreLibrary().getWeakRefFactory(), new WeakReference(object));
}

}

@RubiniusPrimitive(name = "weakref_set_object")
public static abstract class WeakRefSetObjectPrimitiveNode extends RubiniusPrimitiveNode {

public WeakRefSetObjectPrimitiveNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

@Specialization
public Object weakRefSetObject(DynamicObject weakRef, Object object) {
Layouts.WEAK_REF_LAYOUT.setReference(weakRef, new WeakReference(object));
return object;
}

}

@RubiniusPrimitive(name = "weakref_object")
public static abstract class WeakRefObjectPrimitiveNode extends RubiniusPrimitiveNode {

public WeakRefObjectPrimitiveNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

@Specialization
public Object weakRefObject(DynamicObject weakRef) {
return Layouts.WEAK_REF_LAYOUT.getReference(weakRef).get();
}

}

}
Original file line number Diff line number Diff line change
@@ -161,6 +161,8 @@ public class CoreLibrary {
private final DynamicObject fiberErrorClass;
private final DynamicObject threadErrorClass;
private final DynamicObject internalBufferClass;
private final DynamicObject weakRefClass;
private final DynamicObjectFactory weakRefFactory;

private final DynamicObject argv;
private final DynamicObject globalVariablesObject;
@@ -404,6 +406,9 @@ public CoreLibrary(RubyContext context) {
Layouts.CLASS.setInstanceFactoryUnsafe(ioClass, Layouts.IO.createIOShape(ioClass, ioClass));
internalBufferClass = defineClass(ioClass, objectClass, "InternalBuffer");
Layouts.CLASS.setInstanceFactoryUnsafe(internalBufferClass, Layouts.IO_BUFFER.createIOBufferShape(internalBufferClass, internalBufferClass));
weakRefClass = defineClass("WeakRef");
weakRefFactory = Layouts.WEAK_REF_LAYOUT.createWeakRefShape(weakRefClass, weakRefClass);
Layouts.CLASS.setInstanceFactoryUnsafe(weakRefClass, weakRefFactory);

// Modules

@@ -1599,4 +1604,8 @@ public DynamicObjectFactory getHashFactory() {
return hashFactory;
}

public DynamicObjectFactory getWeakRefFactory() {
return weakRefFactory;
}

}
Original file line number Diff line number Diff line change
@@ -50,6 +50,7 @@ public abstract class Layouts {
public static final ThreadLayout THREAD = ThreadLayoutImpl.INSTANCE;
public static final TimeLayout TIME = TimeLayoutImpl.INSTANCE;
public static final UnboundMethodLayout UNBOUND_METHOD = UnboundMethodLayoutImpl.INSTANCE;
public static final WeakRefLayout WEAK_REF_LAYOUT = WeakRefLayoutImpl.INSTANCE;

// Other standard identifiers

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2015 Oracle and/or its affiliates. All rights reserved. This
* code is released under a tri EPL/GPL/LGPL license. You can use it,
* redistribute it and/or modify it under the terms of the:
*
* Eclipse Public License version 1.0
* GNU General Public License version 2
* GNU Lesser General Public License version 2.1
*/
package org.jruby.truffle.runtime.layouts.rubinius;

import com.oracle.truffle.api.object.DynamicObject;
import com.oracle.truffle.api.object.DynamicObjectFactory;
import jnr.ffi.Pointer;
import org.jruby.truffle.om.dsl.api.Layout;
import org.jruby.truffle.runtime.layouts.BasicObjectLayout;

import java.lang.ref.WeakReference;

@Layout
public interface WeakRefLayout extends BasicObjectLayout {

DynamicObjectFactory createWeakRefShape(DynamicObject logicalClass,
DynamicObject metaClass);

DynamicObject createWeakRef(DynamicObjectFactory factory,
WeakReference reference);

WeakReference getReference(DynamicObject object);
void setReference(DynamicObject object, WeakReference reference);

}
2 changes: 1 addition & 1 deletion truffle/src/main/ruby/core.rb
Original file line number Diff line number Diff line change
@@ -96,7 +96,7 @@ def self.omit(reason)
require_relative 'core/rubinius/bootstrap/type'
#require_relative 'core/rubinius/bootstrap/variable_scope'
#require_relative 'core/rubinius/bootstrap/vm'
#require_relative 'core/rubinius/bootstrap/weakref'
require_relative 'core/rubinius/bootstrap/weakref'

# Load platform (ordered according to Rubinius' load_order.txt)

70 changes: 70 additions & 0 deletions truffle/src/main/ruby/core/rubinius/bootstrap/weakref.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Copyright (c) 2007-2015, Evan Phoenix and contributors
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of Rubinius nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

class WeakRef < BasicObject

class RefError < ::RuntimeError; end

def self.new(obj)
Rubinius.primitive :weakref_new
::Kernel.raise PrimitiveFailure, "WeakRef.new primitive failed"
end

def __setobj__(obj)
Rubinius.primitive :weakref_set_object
::Kernel.raise PrimitiveFailure, "WeakRef#__setobj__ primitive failed"
end

def __object__
Rubinius.primitive :weakref_object
::Kernel.raise PrimitiveFailure, "WeakRef#__object__ primitive failed"
end

def __getobj__
obj = __object__()
::Kernel.raise RefError, "Object has been collected as garbage" unless obj
return obj
end

def weakref_alive?
!!__object__
end

def method_missing(method, *args, &block)
target = __getobj__
if target.respond_to?(method)
target.__send__(method, *args, &block)
else
super(method, *args, &block)
end
end

def respond_to_missing?(method, include_private)
target = __getobj__
target.respond_to?(method, include_private) and
(!include_private || target.respond_to?(method, false))
end
end

0 comments on commit abf1edf

Please sign in to comment.