Subject: | [PATCH] Unicode support |
It seems that JavaScript.pm cannot handle javascript unicode strings. The attached patch adds unicode/utf-8 support to the .xs portion of the module. I also added a simple script for the test suite. The patch needs some polishment i.e. it should check if the current perl has unicode support built-in and should fallback to the old behaviour otherwise.
Regards,
Slaven
--- ./t/t80unicode.t.orig 2004-10-21 12:49:53.000000000 +0200
+++ ./t/t80unicode.t 2004-10-21 12:49:37.000000000 +0200
@@ -0,0 +1,12 @@
+use strict;
+use Test;
+use JavaScript;
+
+plan tests => 1;
+
+my $runtime = new JavaScript::Runtime();
+my $context = $runtime->create_context();
+
+my $output = 'a="ab\u20accd";';
+my $perlstring = $context->eval($output);
+ok($perlstring, "ab\x{20ac}cd");
--- ./JavaScript.xs.orig 2001-08-08 22:13:37.000000000 +0200
+++ ./JavaScript.xs 2004-10-21 12:44:36.000000000 +0200
@@ -877,7 +877,27 @@ JSVALToSV(JSContext *cx, JSObject *obj,
} else if(JSVAL_IS_DOUBLE(v)){
sv_setnv(*sv, *JSVAL_TO_DOUBLE(v));
} else if(JSVAL_IS_STRING(v)){
- sv_setpv(*sv, JS_GetStringBytes(JSVAL_TO_STRING(v)));
+ int unilen, i;
+ char *p, *utf8s;
+ jschar *uni = JS_GetStringChars(JSVAL_TO_STRING(v));
+ unilen = 0;
+ for(i = 0; i < JS_GetStringLength(JSVAL_TO_STRING(v)); i++) {
+ unilen += UNISKIP((UV)(uni[i]));
+ }
+ if(unilen == JS_GetStringLength(JSVAL_TO_STRING(v))){
+ sv_setpv(*sv, JS_GetStringBytes(JSVAL_TO_STRING(v)));
+ } else {
+ Newz(1211, utf8s, unilen+1, char);
+ p = utf8s;
+ /* check p! XXX */
+ for(i = 0; i < JS_GetStringLength(JSVAL_TO_STRING(v)); i++) {
+ p = uvchr_to_utf8(p, (UV)(uni[i]));
+ }
+ *p = 0;
+ sv_setpv(*sv, utf8s);
+ Safefree(utf8s);
+ SvUTF8_on(*sv);
+ }
} else {
warn("Unknown primitive type");