[macruby-changes] [2177] MacRuby/branches/experimental

source_changes at macosforge.org source_changes at macosforge.org
Mon Aug 3 10:45:52 PDT 2009


Revision: 2177
          http://trac.macosforge.org/projects/ruby/changeset/2177
Author:   pthomson at apple.com
Date:     2009-08-03 10:45:51 -0700 (Mon, 03 Aug 2009)
Log Message:
-----------
Put in some type-specific coercions from YAML.

Modified Paths:
--------------
    MacRuby/branches/experimental/ext/libyaml/rubyext.c
    MacRuby/branches/experimental/lib/yaml/rubytypes.rb
    MacRuby/branches/experimental/lib/yaml.rb

Modified: MacRuby/branches/experimental/ext/libyaml/rubyext.c
===================================================================
--- MacRuby/branches/experimental/ext/libyaml/rubyext.c	2009-08-03 17:45:50 UTC (rev 2176)
+++ MacRuby/branches/experimental/ext/libyaml/rubyext.c	2009-08-03 17:45:51 UTC (rev 2177)
@@ -20,6 +20,7 @@
 // Ideas to speed this up:
 // Nodes: Stop relying on @document and @node_id as ivars; embed them in a
 // struct that I can access through Data_Get_Struct();
+// Nodes: Cache the tag as a Ruby string
 
 VALUE rb_mYAML;
 VALUE rb_mLibYAML;
@@ -296,7 +297,7 @@
 static VALUE
 rb_yaml_resolver_initialize(VALUE self, SEL sel)
 {
-	rb_ivar_set(self, rb_intern("tags"), rb_hash_new());
+	rb_ivar_set(self, rb_intern("@tags"), rb_hash_new());
 	return self;
 }
 
@@ -307,7 +308,19 @@
 	{
 		case YAML_SCALAR_NODE:
 		{
-			return rb_str_new(node->data.scalar.value, node->data.scalar.length);
+			VALUE tag = rb_str_new2(node->tag);
+			VALUE scalarval = rb_str_new(node->data.scalar.value, node->data.scalar.length);
+			VALUE handler = rb_hash_lookup(tags, tag);
+			if (rb_respond_to(handler, rb_intern("call")))
+			{
+				return rb_funcall(handler, rb_intern("call"), 1, scalarval);
+			}
+			else if (rb_respond_to(handler, rb_intern("yaml_new")))
+			{
+				printf("Calling YAML_NEW\n");
+				return rb_funcall(handler, rb_intern("yaml_new"), 1, scalarval);
+			}
+			return scalarval;
 		}
 		break;
 		case YAML_SEQUENCE_NODE:
@@ -348,7 +361,7 @@
 static VALUE
 rb_yaml_resolver_transfer(VALUE self, SEL sel, VALUE obj)
 {
-	VALUE tags = rb_ivar_get(self, rb_intern("tags"));
+	VALUE tags = rb_ivar_get(self, rb_intern("@tags"));
 	if (rb_obj_is_kind_of(obj, rb_cDocument))
 	{
 		yaml_document_t *document;

Modified: MacRuby/branches/experimental/lib/yaml/rubytypes.rb
===================================================================
--- MacRuby/branches/experimental/lib/yaml/rubytypes.rb	2009-08-03 17:45:50 UTC (rev 2176)
+++ MacRuby/branches/experimental/lib/yaml/rubytypes.rb	2009-08-03 17:45:51 UTC (rev 2177)
@@ -1,5 +1,6 @@
 # -*- mode: ruby; ruby-indent-level: 4; tab-width: 4 -*- vim: sw=4 ts=4
 # require 'date'
+require 'libyaml'
 
 class Class
   def to_yaml(out)
@@ -14,6 +15,7 @@
     klass.define_method(:taguri) do
       @taguri || tag
     end
+    YAML::LibYAML::DEFAULT_RESOLVER.tags[tag] = klass
   end
   
   yaml_as "tag:ruby.yaml.org,2002:object"
@@ -45,10 +47,6 @@
 class String
   yaml_as "tag:yaml.org,2002:str"
   
-  def self.yaml_new(value)
-    value
-  end
-  
   def to_yaml(out)
     out.scalar(taguri, self, self =~ /^:/ ? :quote2 : nil)
   end
@@ -84,6 +82,10 @@
 
 class Integer
   yaml_as "tag:yaml.org,2002:int"
+  def Integer.yaml_new(val)
+    val.to_i
+  end
+  
 	def to_yaml(out)
     out.scalar( "tag:yaml.org,2002:int", self.to_s, :plain )
 	end
@@ -91,6 +93,11 @@
 
 class Float
   yaml_as "tag:yaml.org,2002:float"
+  
+  def Float.yaml_new(val)
+    val.to_f
+  end
+  
   def to_yaml(out)
     str = self.to_s
     if str == "Infinity"

Modified: MacRuby/branches/experimental/lib/yaml.rb
===================================================================
--- MacRuby/branches/experimental/lib/yaml.rb	2009-08-03 17:45:50 UTC (rev 2176)
+++ MacRuby/branches/experimental/lib/yaml.rb	2009-08-03 17:45:51 UTC (rev 2177)
@@ -12,7 +12,7 @@
 module YAML
   
   def YAML.parser
-
+    LibYAML::Parser.new
   end
   
   def YAML.emitter
@@ -35,11 +35,11 @@
   end
   
   def YAML.parse(io)
-
+    LibYAML::Parser.new(io).load
   end
   
   def YAML.parse_file(path)
-
+    File.open(path) { |f| parse(f) }
   end
   
 end
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20090803/79131f20/attachment-0001.html>


More information about the macruby-changes mailing list