ソースを参照

Experimental support for duktape (Step 2/?)

bel 9 年 前
コミット
72204bdc0f
5 ファイル変更83 行追加35 行削除
  1. 37 0
      LICENSE.md
  2. 3 0
      README.md
  3. 4 3
      VS2012/civetweb_lua/civetweb_lua.vcxproj
  4. 7 32
      src/main.c
  5. 32 0
      test/prime.js

+ 37 - 0
LICENSE.md

@@ -171,3 +171,40 @@ LuaXML License
 > LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 > OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 > THE SOFTWARE.
+
+
+Duktape License
+------
+
+### Included only if built with Duktape support.
+
+https://github.com/svaarala/duktape/blob/master/LICENSE.txt
+
+> ===============
+> Duktape license
+> ===============
+> 
+> (http://opensource.org/licenses/MIT)
+> 
+> Copyright (c) 2013-2015 by Duktape authors (see AUTHORS.rst)
+>
+> Permission is hereby granted, free of charge, to any person obtaining a copy
+> of this software and associated documentation files (the "Software"), to deal
+> in the Software without restriction, including without limitation the rights
+> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+> copies of the Software, and to permit persons to whom the Software is
+> furnished to do so, subject to the following conditions:
+>
+> The above copyright notice and this permission notice shall be included in
+> all copies or substantial portions of the Software.
+>
+> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+> THE SOFTWARE.
+
+
+

+ 3 - 0
README.md

@@ -93,6 +93,9 @@ simplicity by a carefully selected list of features:
 <a href=http://viremo.eludi.net/LuaXML/index.html">
 ![LuaXML](https://raw.github.com/civetweb/civetweb/master/resources/luaxml-logo.jpg "LuaXML Logo")
 </a>
+<a href=http://duktape.org">
+![Duktape](https://raw.github.com/civetweb/civetweb/master/resources/duktape-logo.png "Duktape Logo")
+</a>
 
 
 Support

+ 4 - 3
VS2012/civetweb_lua/civetweb_lua.vcxproj

@@ -132,7 +132,7 @@
       </PrecompiledHeader>
       <WarningLevel>Level3</WarningLevel>
       <Optimization>Disabled</Optimization>
-      <PreprocessorDefinitions>USE_IPV6;LUA_COMPAT_ALL;USE_LUA;USE_LUA_SQLITE3;USE_LUA_FILE_SYSTEM;USE_WEBSOCKET;WIN32;CONSOLE;_DEBUG;_WINDOWS;_CRT_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+      <PreprocessorDefinitions>USE_DUKTAPE;USE_IPV6;LUA_COMPAT_ALL;USE_LUA;USE_LUA_SQLITE3;USE_LUA_FILE_SYSTEM;USE_WEBSOCKET;WIN32;CONSOLE;_DEBUG;_WINDOWS;_CRT_SECURE_NO_DEPRECATE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
       <AdditionalIncludeDirectories>$(ProjectDir)..\..\include;$(ProjectDir)..\..\src\third_party;$(ProjectDir)..\..\src\third_party\lua-5.2.4\src;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
     </ClCompile>
     <Link>
@@ -176,7 +176,7 @@
       <Optimization>MaxSpeed</Optimization>
       <FunctionLevelLinking>true</FunctionLevelLinking>
       <IntrinsicFunctions>true</IntrinsicFunctions>
-      <PreprocessorDefinitions>USE_IPV6;LUA_COMPAT_ALL;USE_LUA;USE_LUA_SQLITE3;USE_LUA_FILE_SYSTEM;USE_WEBSOCKET;WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+      <PreprocessorDefinitions>USE_DUKTAPE;USE_IPV6;LUA_COMPAT_ALL;USE_LUA;USE_LUA_SQLITE3;USE_LUA_FILE_SYSTEM;USE_WEBSOCKET;WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
       <AdditionalIncludeDirectories>$(ProjectDir)..\..\include;$(ProjectDir)..\..\src\third_party;$(ProjectDir)..\..\src\third_party\lua-5.2.4\src;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
     </ClCompile>
     <Link>
@@ -229,7 +229,8 @@
     </ProjectReference>
     <ProjectReference Include="..\duktape_lib\duktape_lib.vcxproj">
       <Project>{0A11689C-DB6A-4BF6-97B2-AD32DB863FBD}</Project>
-    </ProjectReference>  </ItemGroup>
+    </ProjectReference>
+  </ItemGroup>
   <ItemGroup>
     <None Include="..\..\src\md5.inl" />
     <None Include="..\..\src\mod_lua.inl" />

+ 7 - 32
src/main.c

@@ -744,48 +744,22 @@ static int run_duktape(const char *file_name)
 	char idx;
 	int ch;
 
+#ifdef WIN32
+	(void)MakeConsole();
+#endif
+
 	ctx = duk_create_heap_default();
 	if (!ctx) {
 		fprintf(stderr, "Failed to create a Duktape heap.\n");
-		exit(1);
+		goto finished;
 	}
 
 	if (duk_peval_file(ctx, file_name) != 0) {
-		fprintf(stderr, "Error: %s\n", duk_safe_to_string(ctx, -1));
+		fprintf(stderr, "%s\n", duk_safe_to_string(ctx, -1));
 		goto finished;
 	}
 	duk_pop(ctx); /* ignore result */
 
-	memset(line, 0, sizeof(line));
-	idx = 0;
-	for (;;) {
-		if (idx >= sizeof(line)) {
-			fprintf(stderr, "Line too long\n");
-			exit(1);
-		}
-
-		ch = fgetc(stdin);
-		if (ch == 0x0a) {
-			line[idx++] = '\0';
-
-			duk_push_global_object(ctx);
-			duk_get_prop_string(ctx, -1 /*index*/, "processLine");
-			duk_push_string(ctx, line);
-			if (duk_pcall(ctx, 1 /*nargs*/) != 0) {
-				fprintf(stderr, "Error: %s\n", duk_safe_to_string(ctx, -1));
-			} else {
-				fprintf(stdout, "%s\n", duk_safe_to_string(ctx, -1));
-			}
-			duk_pop(ctx); /* pop result/error */
-
-			idx = 0;
-		} else if (ch == EOF) {
-			break;
-		} else {
-			line[idx++] = (char)ch;
-		}
-	}
-
 finished:
 	duk_destroy_heap(ctx);
 
@@ -981,6 +955,7 @@ static void start_civetweb(int argc, char *argv[])
 	/* Call Duktape, if -E option is specified */
 	if (argc > 1 && !strcmp(argv[1], "-E")) {
 
+		fprintf(stdout, "asd\n");
 #ifdef USE_DUKTAPE
 		if (argc != 3) {
 			show_usage_and_exit(argv[0]);

+ 32 - 0
test/prime.js

@@ -0,0 +1,32 @@
+// prime.js
+
+// Pure Ecmascript version of low level helper
+function primeCheckEcmascript(val, limit) {
+    for (var i = 2; i <= limit; i++) {
+        if ((val % i) == 0) { return false; }
+    }
+    return true;
+}
+
+// Select available helper at load time
+var primeCheckHelper = (this.primeCheckNative || primeCheckEcmascript);
+
+// Check 'val' for primality
+function primeCheck(val) {
+    if (val == 1 || val == 2) { return true; }
+    var limit = Math.ceil(Math.sqrt(val));
+    while (limit * limit < val) { limit += 1; }
+    return primeCheckHelper(val, limit);
+}
+
+function primeTest() {
+    var res = [];
+
+    print('Have native helper: ' + (primeCheckHelper !== primeCheckEcmascript));
+    for (var i = 2; i <= 1000; i++) {
+        if (primeCheck(i)) { res.push(i); }
+    } 
+    print(res.join(' '));
+}
+
+primeTest();