2 Copyright (C) 2000-2003 SKYRIX Software AG
4 This file is part of OGo
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
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.
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
23 #include "NSString+JS.h"
24 #include "NGJavaScriptContext.h"
25 #import <Foundation/Foundation.h>
26 #include "../common.h"
28 @implementation NSString(NGJavaScript)
30 + (id)stringWithJavaScriptString:(JSString *)_jss {
35 if ((charCount = JS_GetStringLength(_jss)) == 0)
38 return [[[self alloc] initWithJavaScriptString:_jss] autorelease];
41 #if !HANDLE_NSSTRINGS_AS_OBJECTS
42 - (BOOL)_jsGetValue:(jsval *)_value inJSContext:(NGJavaScriptContext *)_ctx {
48 len = [self cStringLength];
50 //NSLog(@"%s: MORPH STRING (len=%i) ...", __PRETTY_FUNCTION__, len);
53 //NSAssert1(cx, @"missing JS context handle (ctx=%@) ...", _ctx);
57 if ((buf = JS_malloc(cx, len + 3)) == NULL) {
58 NSLog(@"%s: could not allocate string buffer (len=%i) ...",
59 __PRETTY_FUNCTION__, len);
62 [self getCString:buf]; buf[len] = '\0';
64 s = JS_NewString(cx, buf, len);
65 *_value = STRING_TO_JSVAL(s);
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");
77 return [NSNumber numberWithInt:[self length]];
80 @end /* NSString(NGJavaScript) */
82 @implementation NSObject(NGJavaScript)
83 /* category on NSObject, so that NSTemporaryString is included ! */
85 - (id)initWithJavaScriptString:(JSString *)_jss {
87 unsigned charCount, i;
96 if ((charCount = JS_GetStringLength(_jss)) == 0) {
101 jchars = JS_GetStringChars(_jss);
102 NSAssert(jchars, @"couldn't get chars of JavaScript string !");
104 uchars = calloc(charCount + 3, sizeof(unichar));
105 for (i = 0; i < charCount; i++)
106 uchars[i] = jchars[i];
108 self = [(NSString *)self initWithCharacters:uchars length:charCount];
115 if ((cstr = JS_GetStringBytes(_jss)))
116 return [(NSString *)self initWithCString:cstr];
118 NSLog(@"ERROR(%s): did not get bytes of JS string 0x%08X !",
119 __PRETTY_FUNCTION__, _jss);
125 @end /* NSObject(NGJavaScript) */