]> err.no Git - sope/blob - Recycler/NGJavaScript/Core+JS.subproj/NSString+JS.m
renamed packages as discussed in the developer list
[sope] / Recycler / NGJavaScript / Core+JS.subproj / NSString+JS.m
1 /*
2   Copyright (C) 2000-2003 SKYRIX Software AG
3
4   This file is part of OGo
5
6   OGo is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with OGo; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21 // $Id$
22
23 #include "NSString+JS.h"
24 #include "NGJavaScriptContext.h"
25 #import <Foundation/Foundation.h>
26 #include "../common.h"
27
28 @implementation NSString(NGJavaScript)
29
30 + (id)stringWithJavaScriptString:(JSString *)_jss {
31   unsigned charCount;
32   
33   if (_jss == NULL)
34     return nil;
35   if ((charCount = JS_GetStringLength(_jss)) == 0)
36     return @"";
37
38   return [[[self alloc] initWithJavaScriptString:_jss] autorelease];
39 }
40
41 #if !HANDLE_NSSTRINGS_AS_OBJECTS
42 - (BOOL)_jsGetValue:(jsval *)_value inJSContext:(NGJavaScriptContext *)_ctx {
43   JSContext *cx;
44   JSString  *s;
45   unsigned  len;
46   char      *buf;
47   
48   len = [self cStringLength];
49   
50   //NSLog(@"%s: MORPH STRING (len=%i) ...", __PRETTY_FUNCTION__, len);
51   
52   cx = [_ctx handle];
53   //NSAssert1(cx, @"missing JS context handle (ctx=%@) ...", _ctx);
54   
55   // TODO: unicode
56   
57   if ((buf = JS_malloc(cx, len + 3)) == NULL) {
58     NSLog(@"%s: could not allocate string buffer (len=%i) ...", 
59           __PRETTY_FUNCTION__, len);
60     return NO;
61   }
62   [self getCString:buf]; buf[len] = '\0';
63   
64   s = JS_NewString(cx, buf, len);
65   *_value = STRING_TO_JSVAL(s);
66   
67   return YES;
68 }
69 #endif
70
71 - (id)_jsprop_length {
72 #if !HANDLE_NSSTRINGS_AS_OBJECTS
73   printf("CALLED NSString 'length' JS property "
74          "(should never happen since NSStrings convert themselves to"
75          " JSString values !)\n");
76 #endif
77   return [NSNumber numberWithInt:[self length]];
78 }
79
80 @end /* NSString(NGJavaScript) */
81
82 @implementation NSObject(NGJavaScript)
83 /* category on NSObject, so that NSTemporaryString is included ! */
84
85 - (id)initWithJavaScriptString:(JSString *)_jss {
86 #if WITH_UNICODE
87   unsigned charCount, i;
88   unichar  *uchars;
89   jschar   *jchars;
90
91   if (_jss == NULL) {
92     [self release];
93     return nil;
94   }
95   
96   if ((charCount = JS_GetStringLength(_jss)) == 0) {
97     [self release];
98     return @"";
99   }
100
101   jchars = JS_GetStringChars(_jss);
102   NSAssert(jchars, @"couldn't get chars of JavaScript string !");
103   
104   uchars = calloc(charCount + 3, sizeof(unichar));
105   for (i = 0; i < charCount; i++)
106     uchars[i] = jchars[i];
107
108   self = [(NSString *)self initWithCharacters:uchars length:charCount];
109   free(uchars);
110   
111   return self;
112 #else
113   unsigned char *cstr;
114
115   if ((cstr = JS_GetStringBytes(_jss)))
116     return [(NSString *)self initWithCString:cstr];
117   
118   NSLog(@"ERROR(%s): did not get bytes of JS string 0x%08X !",
119         __PRETTY_FUNCTION__, _jss);
120   [self release];
121   return nil;
122 #endif
123 }
124
125 @end /* NSObject(NGJavaScript) */