OSDN Git Service

add alert and openFile JavaScript function
[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) setFunctions {
79         [self setFunction:(JSObjectCallAsFunctionCallback) jsAlert withName: "alert"];
80         [self setFunction:(JSObjectCallAsFunctionCallback) jsOpenFile withName: "openFile"];
81 }
82
83 - (void) loadScript {
84         NSString* script;
85         NSString* path = @"./js/charpalette.js";
86         NSError* error;
87         
88         script = [NSString stringWithContentsOfFile: path encoding: NSUTF8StringEncoding error: &error];
89         if (script) {
90                 [self evaluateScript: script];
91                 [self setFunctions];
92         } else {
93                 NSLog(@"the js file is not found");
94         }
95 }
96
97 + (JavaScriptEngine *) instance {
98         static JavaScriptEngine* _instance = nil;
99         
100         if (! _instance) {
101                 _instance = [[JavaScriptEngine alloc] init];
102                 [_instance setContext: JSGlobalContextCreate(NULL)];
103                 [_instance loadScript];
104         }
105         return _instance;
106 }
107
108 - (NSString *) evaluate: (NSString *) script {
109         JSValueRef result = [self evaluateScript: script];
110         
111         CFStringRef resultString;
112         if (result) {
113                 JSStringRef resultStringJS = JSValueToStringCopy(context, result, NULL);
114                 resultString = JSStringCopyCFString(kCFAllocatorDefault, resultStringJS);
115                 JSStringRelease(resultStringJS);
116         } else {
117                 resultString = CFSTR("[Exception]");
118         }
119
120         return [(NSString *) resultString autorelease];
121 }
122
123 - (NSArray *) getFontsNameArray {
124         NSString* v = [self evaluate: @"font"];
125         NSArray* r = [v componentsSeparatedByString: @","];
126
127         return r;
128 }
129
130 - (void) release {
131         JSGlobalContextRelease(context);
132         [super release];
133 }
134
135 @end