OSDN Git Service

implement a function to change font on the glyph view, and add the HOME variable...
[sawarabi-fonts/sawarabi-fonts.git] / chartool / objective-c / JavaScriptEngine.m
1 //
2 //  JavaScriptEngine.m
3 //  chartool
4 //
5 //  Created by mshio on 11/02/06.
6 //  Copyright 2011 mshio. All rights reserved.
7 //
8
9 #include <stdio.h>
10 #import "JavaScriptEngine.h"
11
12 NSString* getNSStringFromParameter(JSContextRef ctx,
13                                                                    JSObjectRef arg,
14                                                                    JSValueRef* jobjExp) {
15         JSStringRef s = JSValueToStringCopy(ctx, arg, jobjExp);
16         size_t bufferSize = JSStringGetMaximumUTF8CStringSize(s);
17         char* buffer = (char*) malloc(bufferSize);
18         JSStringGetUTF8CString(s, buffer, bufferSize);
19         NSString* ret = [[NSString alloc] initWithUTF8String: buffer];
20
21         return ret;
22 }
23
24 static JSValueRef jsAlert(JSContextRef ctx,
25                                                   JSObjectRef jobj,
26                                                   JSObjectRef jobjThis,
27                                                   size_t argLen,
28                                                   const JSObjectRef args[],
29                                                   JSValueRef* jobjExp) {
30         if (argLen) {
31                 NSString* str = getNSStringFromParameter(ctx, args[0], jobjExp);
32
33                 NSAlert * alert = [[NSAlert alloc] init];
34                 [alert setMessageText: str];
35                 [alert runModal];
36         }
37         return JSValueMakeUndefined(ctx);
38 }
39
40 static JSValueRef jsOpenFile(JSContextRef ctx,
41                                                          JSObjectRef jobj,
42                                                          JSObjectRef jobjThis,
43                                                          size_t argLen,
44                                                          const JSObjectRef args[],
45                                                          JSValueRef* jobjExp) {
46         BOOL ret = NO;
47         if (argLen) {
48                 NSString* str = getNSStringFromParameter(ctx, args[0], jobjExp);
49                 
50                 ret = [[NSWorkspace sharedWorkspace] openFile: str];
51         }
52         return JSValueMakeBoolean(ctx, ret);
53 }
54
55
56 @implementation JavaScriptEngine
57
58 - (void) setContext: (JSGlobalContextRef) ctx {
59         context = ctx;
60 }
61
62 - (JSValueRef) evaluateScript: (NSString *) script {
63         JSStringRef scriptJS = JSStringCreateWithCFString((CFStringRef) script);
64         JSValueRef result = JSEvaluateScript(context, scriptJS, NULL, NULL, 0, NULL);
65         JSStringRelease(scriptJS);
66         
67         return result;
68 }
69
70 - (void) setFunction: (JSObjectCallAsFunctionCallback) cfunc withName: (const char*) name {
71         JSObjectRef object = JSContextGetGlobalObject(context);
72         JSStringRef nm = JSStringCreateWithUTF8CString(name);
73         JSObjectRef func = JSObjectMakeFunctionWithCallback(context, nm, cfunc);
74         JSObjectSetProperty(context, object, nm, func, kJSPropertyAttributeNone, NULL);
75         JSStringRelease(nm);
76 }
77
78 - (void) setValue: (const char*) value ToVariable: (const char*) name {
79         JSObjectRef object = JSContextGetGlobalObject(context);
80         JSStringRef nm = JSStringCreateWithUTF8CString(name);
81         JSValueRef var = JSValueMakeString(context, JSStringCreateWithUTF8CString(value));
82         JSObjectSetProperty(context, object, nm, var, kJSPropertyAttributeNone, NULL);
83         JSStringRelease(nm);
84 }
85
86 - (void) setFunctions {
87         [self setFunction:(JSObjectCallAsFunctionCallback) jsAlert withName: "alert"];
88         [self setFunction:(JSObjectCallAsFunctionCallback) jsOpenFile withName: "openFile"];
89 }
90
91 - (void) loadScript {
92         NSString* script;
93         NSString* path = [[NSString alloc] initWithFormat: @"%@/.charpalette.js", NSHomeDirectory()];
94         NSError* error;
95         
96         script = [NSString stringWithContentsOfFile: path encoding: NSUTF8StringEncoding error: &error];
97         if (script) {
98                 [self evaluateScript: script];
99                 [self setFunctions];
100                 [self setValue: [NSHomeDirectory() UTF8String] ToVariable: "HOME"];
101         } else {
102                 NSLog(@"the js file is not found");
103         }
104 }
105
106 + (JavaScriptEngine *) instance {
107         static JavaScriptEngine* _instance = nil;
108         
109         if (! _instance) {
110                 _instance = [[JavaScriptEngine alloc] init];
111                 [_instance setContext: JSGlobalContextCreate(NULL)];
112                 [_instance loadScript];
113         }
114         return _instance;
115 }
116
117 - (NSString *) evaluate: (NSString *) script {
118         JSValueRef result = [self evaluateScript: script];
119         
120         CFStringRef resultString;
121         if (result) {
122                 JSStringRef resultStringJS = JSValueToStringCopy(context, result, NULL);
123                 resultString = JSStringCopyCFString(kCFAllocatorDefault, resultStringJS);
124                 JSStringRelease(resultStringJS);
125         } else {
126                 resultString = CFSTR("[Exception]");
127         }
128
129         return [(NSString *) resultString autorelease];
130 }
131
132 - (NSArray *) getFontsNameArray {
133         NSString* v = [self evaluate: @"font"];
134         if (! [v isEqualToString: @"[Exception]"]) {
135                 NSArray* r = [v componentsSeparatedByString: @","];
136
137                 return r;
138         } else {
139                 return NULL;
140         }
141 }
142
143 - (void) release {
144         JSGlobalContextRelease(context);
145         [super release];
146 }
147
148 @end