v8 javascript SHELL

参照SHELL例子,写了一个类似编译器一样的东西,其中加了GBK->UTF8转码..可以用EDITPLUS直接写JAVASCRIPT了..

v8shell-3.19.9.7z

  1 #include <windows.h>
  2 #include <iostream>
  3 #include <algorithm>
  4 #include <string>
  5 #include <sstream>
  6 #include <cstdlib>
  7 #include <cstdio>
  8 #include <cstring>
  9 #include <cassert>
 10 
 11 #include <v8.h>
 12 
 13 using namespace v8;
 14 using namespace std;
 15 
 16 
 17 
 18 // GBK->UTF-8    (WIN32)
 19 Handle<String> ConvertGBKToUTF8( const char* str )
 20 {
 21     int alen;
 22     if ( str == NULL || ( alen = strlen( str ) ) == 0 )
 23     {
 24         return Handle<String>();
 25     }
 26 
 27     int wlen = MultiByteToWideChar( CP_ACP, 0, str, alen, NULL, 0 );
 28     if ( wlen == 0 )
 29     {
 30         return Handle<String>();
 31     }
 32 
 33     wchar_t* lpw = ( wchar_t* )malloc( wlen * sizeof( wchar_t ) );
 34     if ( lpw == NULL )
 35     {
 36         return Handle<String>();
 37     }
 38 
 39     int ret = MultiByteToWideChar( CP_ACP, 0, str, alen, lpw, wlen );
 40     if ( ret != wlen )
 41     {
 42         free( lpw );
 43         return Handle<String>();
 44     }
 45 
 46     alen = WideCharToMultiByte( CP_UTF8, 0, lpw, wlen, NULL, 0, NULL, NULL );
 47     if ( alen == 0 )
 48     {
 49         free( lpw );
 50         return Handle<String>();
 51     }
 52 
 53     char* lpa = ( char* )calloc( 1, ( alen + 1 ) * sizeof( char ) );
 54     if ( lpa == NULL )
 55     {
 56         free( lpw );
 57         return Handle<String>();
 58     }
 59 
 60     ret = WideCharToMultiByte ( CP_UTF8, 0, lpw, wlen, lpa, alen, NULL, NULL );
 61     if ( ret != alen )
 62     {
 63         free( lpw );
 64         free( lpa );
 65         return Handle<String>();
 66     }
 67 
 68     Handle<String> utf8str = String::New( lpa );
 69 
 70     free( lpw );
 71     free( lpa );
 72     return utf8str;
 73 }
 74 
 75 // 读取文件,并由GBK改为UTF-8    (WIN32)
 76 Handle<String> ReadScript( const char* lpszFile )
 77 {
 78     HANDLE hFile = ::CreateFile( lpszFile, GENERIC_READ, FILE_SHARE_READ, \
 79                                  NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
 80     if ( hFile == INVALID_HANDLE_VALUE )
 81     {
 82         return Handle<String>();
 83     }
 84 
 85     DWORD dwFileSize = ::GetFileSize( hFile, NULL );
 86     if ( dwFileSize == INVALID_FILE_SIZE )
 87     {
 88         ::CloseHandle( hFile );
 89         return Handle<String>();
 90     }
 91 
 92     char* lpData = ( char* )calloc( 1, ( dwFileSize + 1 ) * sizeof( char ) );
 93     if ( lpData == NULL )
 94     {
 95         ::CloseHandle( hFile );
 96         return Handle<String>();
 97     }
 98 
 99     DWORD dwBytes;
100     BOOL bRet = ::ReadFile( hFile, ( LPVOID )lpData, dwFileSize, &dwBytes, NULL );
101     if ( !bRet || dwBytes != dwFileSize )
102     {
103         free( lpData );
104         ::CloseHandle( hFile );
105         return Handle<String>();
106     }
107 
108     ::CloseHandle( hFile );
109 
110     Handle<String> scriptText = ConvertGBKToUTF8( lpData );
111 
112     free( lpData );
113 
114     return scriptText;
115 }
116 
117 
118 // UTF-8->GBK    (WIN32)
119 string ConvertUTF8ToGBK( const char* str )
120 {
121     string gbk;
122 
123     int alen;
124     if ( str == NULL || ( alen = strlen( str ) ) == 0 )
125     {
126         return gbk;
127     }
128 
129     int wlen = MultiByteToWideChar( CP_UTF8, 0, str, alen, NULL, 0 );
130     if ( wlen == 0 )
131     {
132         return gbk;
133     }
134 
135     wchar_t* lpw = ( wchar_t* )malloc( wlen * sizeof( wchar_t ) );
136     if ( lpw == NULL )
137     {
138         return gbk;
139     }
140 
141     int ret = MultiByteToWideChar( CP_UTF8, 0, str, alen, lpw, wlen );
142     if ( ret != wlen )
143     {
144         free( lpw );
145         return gbk;
146     }
147 
148     alen = WideCharToMultiByte( CP_ACP, 0, lpw, wlen, NULL, 0, NULL, NULL );
149     if ( alen == 0 )
150     {
151         free( lpw );
152         return gbk;
153     }
154 
155     char* lpa = ( char* )malloc( alen * sizeof( char ) );
156     if ( lpa == NULL )
157     {
158         free( lpw );
159         return gbk;
160     }
161 
162     ret = WideCharToMultiByte ( CP_ACP, 0, lpw, wlen, lpa, alen, NULL, NULL );
163     if ( ret != alen )
164     {
165         free( lpw );
166         free( lpa );
167         return gbk;
168     }
169 
170     gbk.assign( lpa, alen );
171 
172     free( lpw );
173     free( lpa );
174     return gbk;
175 }
176 
177 
178 
179 Handle<Value> Print( const Arguments& args )
180 {
181     bool first = true;
182     Isolate* isolate = args.GetIsolate();
183     stringstream ss;
184     int cnt = 0;
185 
186     int len = args.Length();
187     for ( int i = 0; i < len; i ++ )
188     {
189         HandleScope handle_scope( isolate );
190         if ( first )
191         {
192             first = false;
193         }
194         else
195         {
196             ss << " ";
197         }
198 
199         cnt ++;
200 
201         if ( args[i]->IsString() )
202         {
203             ss << ConvertUTF8ToGBK( *String::Utf8Value( args[i] ) );
204         }
205         else if ( args[i]->IsInt32() )
206         {
207             ss << args[i]->Int32Value();
208         }
209         else if ( args[i]->IsNumber() )
210         {
211             ss << args[i]->NumberValue();
212         }
213         else if ( args[i]->IsBoolean() )
214         {
215             if ( args[i]->BooleanValue() )
216             {
217                 ss << "true";
218             }
219             else
220             {
221                 ss << "false";
222             }
223         }
224         else
225         {
226             ss << "<undefined>";
227             cnt --;
228         }
229     }
230 
231     cout << ss.str() << endl;
232 
233     return Integer::New( cnt );
234 }
235 
236 
237 void ReportException( Isolate* isolate, TryCatch* try_catch )
238 {
239     HandleScope handle_scope( isolate );
240     String::Utf8Value exception( try_catch->Exception() );
241     Handle<Message> message = try_catch->Message();
242     stringstream ss;
243 
244     if ( message.IsEmpty() )
245     {
246         cerr << ConvertUTF8ToGBK( *exception ) << endl;
247         return;
248     }
249 
250 
251     String::Utf8Value filename( message->GetScriptResourceName() );
252     ss << ConvertUTF8ToGBK( *filename ) << ':';
253     ss << message->GetLineNumber() << ':';
254     ss << ConvertUTF8ToGBK( *exception ) << '\n';
255 
256     Handle<Value> stack = try_catch->StackTrace();
257     if ( !stack.IsEmpty() )
258     {
259         String::Utf8Value sstack( stack );
260         ss << ConvertUTF8ToGBK( *sstack ) << '\n';
261     }
262 
263     cout << ss.str();
264 }
265 
266 // 版本信息
267 Handle<Value> Version( const Arguments& args )
268 {
269     return String::New( V8::GetVersion() );
270 }
271 
272 // 压入自定义函数, 并创建上下文
273 Handle<Context> CreateContext( Isolate* isolate )
274 {
275     Handle<ObjectTemplate> global = ObjectTemplate::New();
276     global->Set( String::New( "print" ), FunctionTemplate::New( Print ) );
277     global->Set( String::New( "version" ), FunctionTemplate::New( Version ) );
278 
279     return Context::New( isolate, NULL, global );
280 }
281 
282 
283 int main( int argc, char* argv[] )
284 {
285     if ( argc == 1 )
286     {
287         return -1;
288     }
289 
290     V8::SetFlagsFromCommandLine( &argc, argv, true );
291     Isolate* isolate = Isolate::GetCurrent();
292 
293     {
294         HandleScope handle_scope( isolate );
295         Handle<Context> context = CreateContext( isolate );
296         if ( context.IsEmpty() )
297         {
298             fprintf( stderr, "Error creating context\n" );
299             return 1;
300         }
301 
302         Context::Scope context_scope( context );
303 
304         Handle<String> file_name = ConvertGBKToUTF8( argv[1] );
305 
306         Handle<String> source = ReadScript( argv[1] );
307         if ( !source.IsEmpty() )
308         {
309             TryCatch try_catch;
310             Handle<Script> script = Script::Compile( source, file_name );
311             if ( !script.IsEmpty() )
312             {
313                 Handle<Value> result = script->Run();
314                 if ( result.IsEmpty() )
315                 {
316                     ReportException( isolate, &try_catch );
317                 }
318             }
319             else
320             {
321                 ReportException( isolate, &try_catch );
322             }
323         }
324     }
325 
326     V8::Dispose();
327     return 0;
328 }
原文地址:https://www.cnblogs.com/javado/p/3122970.html