Toby Inkster avatar Toby Inkster committed f60d8b8

quick port from JavaScript::SpiderMonkey; release 0.090_02; then bugfix for &parameters().

Comments (0)

Files changed (11)

 Bug tracker:  <http://rt.cpan.org/Dist/Display.html?Queue=JSON-T>
 Maintainer:   Toby Inkster <mailto:tobyink@cpan.org>
 
+0.090_02 [2010-08-02]
+ - Switched from JavaScript::SpiderMonkey to JE - SpiderMonkey version is
+   retained for those that want it.
+
 0.090_01 [2010-07-30]
  - (Bugfix) Non-JSON-string input for transform method was not being
    correctly converted to JSON.
 		dcs:item   [ a dcs:Bugfix ; rdfs:label "Non-JSON-string input for transform method was not being correctly converted to JSON."@en ] 
 		] .
 
+my:v_0-090_02
+
+	a               :Version ;
+	dc:issued       "2010-08-02"^^xsd:date ;
+	:revision       "0.090_02"^^xsd:string ;
+	:file-release   <http://backpan.cpan.org/authors/id/T/TO/TOBYINK/JSON-T-0.090_02.tar.gz> ;
+	dcs:changeset [
+		dcs:versus my:v_0-090_01 ;
+		dcs:item   [ rdfs:label "Switched from JavaScript::SpiderMonkey to JE - SpiderMonkey version is retained for those that want it."@en ] 
+		] .
+
 #############################################################
 
 my:project
 	:tester         toby:i ;
 	:created        "2010-07-29"^^xsd:date ;
 	:license        <http://creativecommons.org/licenses/LGPL/2.1/> ;
-	:release        my:v_0-090_00 , my:v_0-090_01 .
+	:release        my:v_0-090_00 , my:v_0-090_01 , my:v_0-090_02 .
 
 #############################################################
 
 <?xml version="1.0" encoding="utf-8"?>
 <rdf:RDF xmlns:dbug="http://ontologi.es/doap-bugs#" xmlns:dc="http://purl.org/dc/terms/" xmlns:dcs="http://ontologi.es/doap-changeset#" xmlns:doap="http://usefulinc.com/ns/doap#" xmlns:foaf="http://xmlns.com/foaf/0.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
-<rdf:Description rdf:nodeID="r230E4B669C2211DFB6E6CDE601C96878r0">
-	<dcs:item rdf:nodeID="r230E4B669C2211DFB6E6CDE601C96878r1"/>
+<rdf:Description rdf:nodeID="rD199374A9E7F11DFAE75977ABD853821r0">
+	<dcs:item rdf:nodeID="rD199374A9E7F11DFAE75977ABD853821r1"/>
 	<dcs:versus rdf:resource="http://purl.org/NET/cpan-uri/dist/JSON-T/v_0-090_00"/>
 </rdf:Description>
-<rdf:Description rdf:nodeID="r230E4B669C2211DFB6E6CDE601C96878r1">
+<rdf:Description rdf:nodeID="rD199374A9E7F11DFAE75977ABD853821r1">
 	<rdf:type rdf:resource="http://ontologi.es/doap-changeset#Bugfix"/>
 	<rdfs:label xml:lang="en">Non-JSON-string input for transform method was not being correctly converted to JSON.</rdfs:label>
 </rdf:Description>
-<rdf:Description rdf:nodeID="r230E4B669C2211DFB6E6CDE601C96878r2">
+<rdf:Description rdf:nodeID="rD199374A9E7F11DFAE75977ABD853821r2">
+	<dcs:item rdf:nodeID="rD199374A9E7F11DFAE75977ABD853821r3"/>
+	<dcs:versus rdf:resource="http://purl.org/NET/cpan-uri/dist/JSON-T/v_0-090_01"/>
+</rdf:Description>
+<rdf:Description rdf:nodeID="rD199374A9E7F11DFAE75977ABD853821r3">
+	<rdfs:label xml:lang="en">Switched from JavaScript::SpiderMonkey to JE - SpiderMonkey version is retained for those that want it.</rdfs:label>
+</rdf:Description>
+<rdf:Description rdf:nodeID="rD199374A9E7F11DFAE75977ABD853821r4">
 	<doap:browse rdf:resource="http://goddamn.co.uk/viewvc/perlmods/JSON-T/"/>
 	<rdf:type rdf:resource="http://usefulinc.com/ns/doap#SVNRepository"/>
 </rdf:Description>
 	<doap:programming-language>Perl</doap:programming-language>
 	<doap:release rdf:resource="http://purl.org/NET/cpan-uri/dist/JSON-T/v_0-090_00"/>
 	<doap:release rdf:resource="http://purl.org/NET/cpan-uri/dist/JSON-T/v_0-090_01"/>
-	<doap:repository rdf:nodeID="r230E4B669C2211DFB6E6CDE601C96878r2"/>
+	<doap:release rdf:resource="http://purl.org/NET/cpan-uri/dist/JSON-T/v_0-090_02"/>
+	<doap:repository rdf:nodeID="rD199374A9E7F11DFAE75977ABD853821r4"/>
 	<doap:shortdesc xml:lang="en">transform JSON using JsonT</doap:shortdesc>
 	<doap:tester rdf:resource="http://tobyinkster.co.uk/#i"/>
 	<rdf:type rdf:resource="http://usefulinc.com/ns/doap#Project"/>
 	<rdfs:comment xml:lang="en">Original version</rdfs:comment>
 </rdf:Description>
 <rdf:Description rdf:about="http://purl.org/NET/cpan-uri/dist/JSON-T/v_0-090_01">
-	<dcs:changeset rdf:nodeID="r230E4B669C2211DFB6E6CDE601C96878r0"/>
+	<dcs:changeset rdf:nodeID="rD199374A9E7F11DFAE75977ABD853821r0"/>
 	<dc:issued rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2010-07-30</dc:issued>
 	<doap:file-release rdf:resource="http://backpan.cpan.org/authors/id/T/TO/TOBYINK/JSON-T-0.090_01.tar.gz"/>
 	<doap:revision rdf:datatype="http://www.w3.org/2001/XMLSchema#string">0.090_01</doap:revision>
 	<rdf:type rdf:resource="http://usefulinc.com/ns/doap#Version"/>
 </rdf:Description>
+<rdf:Description rdf:about="http://purl.org/NET/cpan-uri/dist/JSON-T/v_0-090_02">
+	<dcs:changeset rdf:nodeID="rD199374A9E7F11DFAE75977ABD853821r2"/>
+	<dc:issued rdf:datatype="http://www.w3.org/2001/XMLSchema#date">2010-08-02</dc:issued>
+	<doap:file-release rdf:resource="http://backpan.cpan.org/authors/id/T/TO/TOBYINK/JSON-T-0.090_02.tar.gz"/>
+	<doap:revision rdf:datatype="http://www.w3.org/2001/XMLSchema#string">0.090_02</doap:revision>
+	<rdf:type rdf:resource="http://usefulinc.com/ns/doap#Version"/>
+</rdf:Description>
 <rdf:Description xmlns:ns1="http://www.w3.org/2002/07/owl#" rdf:about="http://tobyinkster.co.uk/#i">
 	<rdf:type rdf:resource="http://xmlns.com/foaf/0.1/Person"/>
 	<ns1:sameAs rdf:resource="http://purl.org/NET/cpan-uri/person/tobyink"/>

Binary file added.

 inc/Test/Signature.pm
 
 lib/JSON/T.pm
+lib/JSON/T/SpiderMonkey.pm
   ExtUtils::MakeMaker: 6.42
 distribution_type: module
 generated_by: 'Module::Install version 0.95'
+keywords:
+  - JSON
+  - transform
+  - JsonT
+  - Javascript
 license: lgpl
 meta-spec:
   url: http://module-build.sourceforge.net/META-spec-v1.4.html
     - inc
     - t
 requires:
+  JE: 0.049
   JSON: 2.00
-  JavaScript::SpiderMonkey: 0.19
   common::sense: 1.00
+  perl: 5.8.0
 resources:
   bugtracker: http://rt.cpan.org/Dist/Display.html?Queue=JSON-T
   homepage: http://search.cpan.org/dist/JSON-T/
   license: http://opensource.org/licenses/lgpl-license.php
   repository: http://goddamn.co.uk/viewvc/perlmods/JSON-T/
-version: 0.090
-
+version: 0.090_02
 license             'lgpl';
 
 requires            'common::sense'      => '1.00';
-requires            'JavaScript::SpiderMonkey' => '0.19';
+requires            'JE'                 => '0.049';
 requires            'JSON'               => '2.00';
 test_requires       'Test::More'         => '0.61';
 
+keywords 'JSON', 'transform', 'JsonT', 'Javascript';
+
 resources(
 	'homepage'   => "http://search.cpan.org/dist/$dist/",
 	'repository' => "http://goddamn.co.uk/viewvc/perlmods/$dist/",
     structures, analogous to XSLT in the XML world.
 
     JsonT is described at <http://goessner.net/articles/jsont/>. JsonT is a
-    profile of Javascript; this module uses JavaScript::SpiderMonkey for
-    Javascript support.
+    profile of Javascript; this module uses the pure Perl Javascript
+    implementation JE for Javascript support. An alternative implementation
+    using Mozilla's libjs is provided as JSON::T::SpiderMonkey.
 
     This module provides a similar API to XML::Saxon::XSLT2.
 
         will fail if the output is not a JSON string.
 
 Javascript Execution Environment
-    JSON::T is a profile of Javascript. This module runs scripts via
-    JavaScript::SpiderMonkey. As this is not a browser environment, many
-    global objects familiar to browser Javascript developers are not
-    available.
+    JSON::T is a profile of Javascript. This module runs scripts via JE. As
+    this is not a browser environment, many global objects familiar to
+    browser Javascript developers are not available.
 
     A single global object called "JSON" is provided with methods
     "stringify" and "parse" compatible with the well-known json2.js library
 SEE ALSO
     Specification: <http://goessner.net/articles/jsont/>.
 
-    Related modules: JSON, JSON::Path, JSON::GRDDL.
+    Related modules: JSON, JSON::Path, JSON::GRDDL, JSON::Hyper,
+    JSON::Schema.
 
-    Requires: JSON, JavaScript::SpiderMonkey.
+    Requires: JSON, JE.
 
 AUTHOR
     Toby Inkster <tobyink@cpan.org>.
 JSON
 
 my $jsont = <<'JSONT';
+var namePrefix = "";
+
 var People =
 {
 	"self" : function(x)
 				"http://xmlns.com/foaf/0.1/name" :
 				[{
 					"type" : "literal" ,
-					"value" : x.name
+					"value" : namePrefix + x.name
 				}],
 				"http://xmlns.com/foaf/0.1/mbox" :
 				[{
 JSONT
 
 my $T = JSON::T->new($jsont);
+## $T->parameters(namePrefix => 'Mr ');
 say Dumper($T->transform_structure($json));
 package JSON::T;
 
+use 5.008;
 use common::sense;
 use overload '""' => \&to_string;
 
-use JavaScript::SpiderMonkey;
+use JE;
 use JSON;
 
 our $JSLIB;
-our $VERSION = '0.090';
+our $VERSION = '0.090_02';
 
 sub new
 {
 		$JSLIB = <DATA>;
 	}
 
-	my $JS = JavaScript::SpiderMonkey->new();
+	my $JS = JE->new;
 	my $self = bless {
 		code           => $transformation_code ,
 		name           => $transformation_name ,
 		messages       => [],
 		}, $class;
 
-	$JS->init;
-	$JS->function_set("return_to_perl", sub
+	$JS->new_function("return_to_perl", sub
 		{
 			my $v = shift;
 			$self->{'output'} = $v;
 		});
-	$JS->function_set("print_to_perl", sub
+	$JS->new_function("print_to_perl", sub
 		{
 			print @_;
 		});
 		{
 			$v = $v->[1];
 		}
-		$self->{'engine'}->property_by_path($k, "$v");
+		$self->{'engine'}->eval("var $k;");
+		$self->{'engine'}->eval($k)->set(
+			JE::Object::String->new($self->{'engine'}, $v)
+			);
 	}
 }
 
 structures, analogous to XSLT in the XML world.
 
 JsonT is described at L<http://goessner.net/articles/jsont/>. JsonT is
-a profile of Javascript; this module uses L<JavaScript::SpiderMonkey>
-for Javascript support.
+a profile of Javascript; this module uses the pure Perl Javascript
+implementation L<JE> for Javascript support. An alternative implementation
+using Mozilla's libjs is provided as L<JSON::T::SpiderMonkey>.
 
 This module provides a similar API to L<XML::Saxon::XSLT2>.
 
 =head1 Javascript Execution Environment
 
 JSON::T is a profile of Javascript. This module runs scripts via
-L<JavaScript::SpiderMonkey>. As this is not a browser environment,
-many global objects familiar to browser Javascript developers are
-not available.
+L<JE>. As this is not a browser environment, many global objects
+familiar to browser Javascript developers are not available.
 
 A single global object called "JSON" is provided with methods
 C<stringify> and C<parse> compatible with the well-known json2.js
 Related modules: L<JSON>, L<JSON::Path>, L<JSON::GRDDL>,
 L<JSON::Hyper>, L<JSON::Schema>.
 
-Requires: L<JSON>, L<JavaScript::SpiderMonkey>.
+Requires: L<JSON>, L<JE>.
 
 =head1 AUTHOR
 

lib/JSON/T/SpiderMonkey.pm

-package JSON::T;
+package JSON::T::SpiderMonkey;
 
+use 5.008;
+use base qw[JSON::T];
 use common::sense;
 use overload '""' => \&to_string;
 
 use JSON;
 
 our $JSLIB;
-our $VERSION = '0.090';
+our $VERSION = '0.090_02';
 
 sub new
 {
 	return $self;
 }
 
-sub to_string
-{
-	my ($self) = @_;
-	return 'JsonT:#'.$self->{'name'};
-}
-
 sub parameters
 {
 	my ($self, %args) = @_;
 	}
 }
 
-sub transform
-{
-	my ($self, $input) = @_;
-	
-	if (ref $input)
-	{
-		$input = to_json($input);
-	}
-	
-	my $name = $self->{'name'};
-	my $rv1  = $self->{'engine'}->eval("return_to_perl(JSON.transform($input, $name));");
-
-	return $self->{'output'};
-}
-
-sub transform_structure
-{
-	my ($self, $input) = @_;
-	my $output = $self->transform($input);
-	return from_json($output);
-}
-*transform_document = \&transform_structure;
-
-# none of this is useful, but provided for XML::Saxon::XSLT2 compat.
-sub messages
-{
-	return;
-}
-sub media_type
-{
-	my ($self, $default) = @_;
-	return $default;
-}
-*version        = \&media_type;
-*doctype_system = \&media_type;
-*doctype_public = \&media_type;
-*encoding       = \&media_type;
-
 1;
 
 =head1 NAME
 
-JSON::T - transform JSON using JsonT
-
-=head1 SYNOPSIS
-
- my $jsont = slurp('foo/bar.js');
- my $input = slurp('foo/quux.json');
- my $JSONT = JSON::T->new($jsont);
- print $JSONT->transform($input);
+JSON::T::SpiderMonkey - transform JSON using JsonT and SpiderMonkey (libjs)
 
 =head1 DESCRIPTION
 
-This module implements JsonT, a language for transforming JSON-like
-structures, analogous to XSLT in the XML world.
-
-JsonT is described at L<http://goessner.net/articles/jsont/>. JsonT is
-a profile of Javascript; this module uses L<JavaScript::SpiderMonkey>
-for Javascript support.
-
-This module provides a similar API to L<XML::Saxon::XSLT2>.
-
-=head1 Constructor
-
-=over 4
-
-=item C<< JSON::T->new($code, $name) >>
-
-Constructs a new JSON::T transformation. $code is the JsonT Javascript
-code. As a JsonT file can contain multiple (potentially unrelated)
-transformations, the name of the particular transformation you want to
-use should also be provided. If $name is omitted, then the name "_main"
-is assumed.
-
-=back
-
-=head1 Methods
-
-=over 4
-
-=item C<< parameters(param1=>$arg1, param2=>$arg2, ...) >>
-
-Sets global variables available to the Javascript code. All arguments
-are treated as strings.
-
-=item C<< transform($input) >>
-
-Run the transformation. The input may be a JSON string, or a native Perl
-nested arrayref/hashref structure, in which case it will be stringified
-using the JSON module's to_json function. The output (return value) will
-be a string.
-
-=item C<< transform_structure($input) >>
-
-Like C<transform>, but attempts to parse the output as a JSON string and
-return a native Perl arrayref/hashref structure. This method will fail
-if the output is not a JSON string.
-
-=back
-
-=head1 Javascript Execution Environment
-
-JSON::T is a profile of Javascript. This module runs scripts via
-L<JavaScript::SpiderMonkey>. As this is not a browser environment,
-many global objects familiar to browser Javascript developers are
-not available.
-
-A single global object called "JSON" is provided with methods
-C<stringify> and C<parse> compatible with the well-known json2.js
-library (L<http://www.JSON.org/json2.js>), and a method
-C<transform(obj,jsont)> that provides a Javascript JsonT
-implementation.
-
-A function C<print_to_perl> is provided which prints to Perl's
-STDOUT stream.
+This module uses L<JavaScript::SpiderMonkey> to provide JavaScript support;
+otherwise it's the same as L<JSON::T>.
 
 =head1 BUGS
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.