Quellcode durchsuchen

Duktape: Store connection object as internal property on the Ecmascript function object

According to hint in https://github.com/svaarala/duktape/issues/392#issuecomment-145301651
bel vor 9 Jahren
Ursprung
Commit
799a8e0f19
3 geänderte Dateien mit 23 neuen und 3 gelöschten Zeilen
  1. 2 2
      src/mod_duktape.inl
  2. 20 0
      test/page2.ssjs
  3. 1 1
      test/prime.ssjs

+ 2 - 2
src/mod_duktape.inl

@@ -128,14 +128,14 @@ mg_exec_duktape_script(struct mg_connection *conn, const char *path)
 	duk_push_object(ctx); /* create a new table/object ("conn") */
 
 	duk_push_c_function(ctx, duk_itf_write, 1 /* 1 = nargs */);
-	duk_put_prop_string(ctx, -2, "write"); /* add function conn.write */
 	duk_push_pointer(ctx, (void *)conn);
 	duk_put_prop_string(ctx, -2, civetweb_conn_id);
+	duk_put_prop_string(ctx, -2, "write"); /* add function conn.write */
 
 	duk_push_c_function(ctx, duk_itf_read, 0 /* 0 = nargs */);
-	duk_put_prop_string(ctx, -2, "read"); /* add function conn.read */
 	duk_push_pointer(ctx, (void *)conn);
 	duk_put_prop_string(ctx, -2, civetweb_conn_id);
+	duk_put_prop_string(ctx, -2, "read"); /* add function conn.read */
 
 	duk_put_prop_string(ctx, -2, "conn"); /* call the table "conn" */
 

+ 20 - 0
test/page2.ssjs

@@ -0,0 +1,20 @@
+conn.write("HTTP/1.0 200 OK\r\n")
+conn.write("Content-Type: text/html\r\n")
+conn.write("\r\n")
+conn.write("<html><body>\r\n")
+conn.write("<p>This is an example of a server side JavaScript, served by the ")
+conn.write('<a href="https://github.com/civetweb/civetweb/">CivetWeb web server</a>.')
+conn.write("</p>\r\n<p>")
+
+
+elms = Object.getOwnPropertyNames(conn)
+
+for (var i = 0; i < elms.length; i++) {
+  conn.write(JSON.stringify(elms[i]))
+  conn.write(JSON.stringify(Object.getOwnPropertyDescriptor(conn, elms[i])))
+  conn.write("<br>\r\n")
+}
+
+
+conn.write('</p>\r\n')
+conn.write('</body></html>\r\n')

+ 1 - 1
test/prime.ssjs

@@ -22,7 +22,7 @@ function primeCheck(val) {
 function primeTest() {
     var res = [];
 
-    print('Have native helper: ' + (primeCheckHelper !== primeCheckEcmascript));
+    print('Have native helper: ' + (primeCheckHelper !== primeCheckEcmascript) + '\n');
     for (var i = 2; i <= 1000; i++) {
         if (primeCheck(i)) { res.push(i); }
     }