コード例 #1
0
ファイル: duk_console.c プロジェクト: oleavr/duktape
void duk_console_init(duk_context *ctx, duk_uint_t flags) {
	duk_push_object(ctx);

	/* Custom function to format objects; user can replace.
	 * For now, try JX-formatting and if that fails, fall back
	 * to ToString(v).
	 */
	duk_eval_string(ctx,
		"(function format(v){"
		    "try{"
		        "return Duktape.enc('jx',v);"
		    "}catch(e){"
		        "return ''+v;"
		    "}"
		"})");
	duk_put_prop_string(ctx, -2, "format");

	duk__console_reg_vararg_func(ctx, duk__console_assert, "assert");
	duk__console_reg_vararg_func(ctx, duk__console_log, "log");
	duk__console_reg_vararg_func(ctx, duk__console_log, "debug");  /* alias to console.log */
	duk__console_reg_vararg_func(ctx, duk__console_trace, "trace");
	duk__console_reg_vararg_func(ctx, duk__console_info, "info");
	duk__console_reg_vararg_func(ctx, duk__console_warn, "warn");
	duk__console_reg_vararg_func(ctx, duk__console_error, "error");
	duk__console_reg_vararg_func(ctx, duk__console_error, "exception");  /* alias to console.error */
	duk__console_reg_vararg_func(ctx, duk__console_dir, "dir");

	duk_put_global_string(ctx, "console");

	/* Proxy wrapping: ensures any undefined console method calls are
	 * ignored silently.  This is required specifically by the
	 * DeveloperToolsWG proposal (and is implemented also by Firefox:
	 * https://bugzilla.mozilla.org/show_bug.cgi?id=629607).
	 */

	if (flags & DUK_CONSOLE_PROXY_WRAPPER) {
		/* Tolerate errors: Proxy may be disabled. */
		duk_peval_string_noresult(ctx,
			"(function(){"
			    "var D=function(){};"
			    "console=new Proxy(console,{"
			        "get:function(t,k){"
			            "var v=t[k];"
			            "return typeof v==='function'?v:D;"
			        "}"
			    "});"
			"})();"
		);
	}
}
コード例 #2
0
ファイル: duk_console.c プロジェクト: svaarala/duktape
void duk_console_init(duk_context *ctx, duk_uint_t flags) {
	duk_uint_t flags_orig;

	/* If both DUK_CONSOLE_STDOUT_ONLY and DUK_CONSOLE_STDERR_ONLY where specified,
	 * just turn off DUK_CONSOLE_STDOUT_ONLY and keep DUK_CONSOLE_STDERR_ONLY.
	 */
	if ((flags & DUK_CONSOLE_STDOUT_ONLY) && (flags & DUK_CONSOLE_STDERR_ONLY)) {
	    flags &= ~DUK_CONSOLE_STDOUT_ONLY;
	}
	/* Remember the (possibly corrected) flags we received. */
	flags_orig = flags;

	duk_push_object(ctx);

	/* Custom function to format objects; user can replace.
	 * For now, try JX-formatting and if that fails, fall back
	 * to ToString(v).
	 */
	duk_eval_string(ctx,
		"(function (E) {"
		    "return function format(v){"
		        "try{"
		            "return E('jx',v);"
		        "}catch(e){"
		            "return String(v);"  /* String() allows symbols, ToString() internal algorithm doesn't. */
		        "}"
		    "};"
		"})(Duktape.enc)");
	duk_put_prop_string(ctx, -2, "format");

	flags = flags_orig;
	if (!(flags & DUK_CONSOLE_STDOUT_ONLY) && !(flags & DUK_CONSOLE_STDERR_ONLY)) {
	    /* No output indicators were specified; these levels go to stdout. */
	    flags |= DUK_CONSOLE_STDOUT_ONLY;
	}
	duk__console_reg_vararg_func(ctx, duk__console_assert, "assert", flags);
	duk__console_reg_vararg_func(ctx, duk__console_log, "log", flags);
	duk__console_reg_vararg_func(ctx, duk__console_log, "debug", flags);  /* alias to console.log */
	duk__console_reg_vararg_func(ctx, duk__console_trace, "trace", flags);
	duk__console_reg_vararg_func(ctx, duk__console_info, "info", flags);

	flags = flags_orig;
	if (!(flags & DUK_CONSOLE_STDOUT_ONLY) && !(flags & DUK_CONSOLE_STDERR_ONLY)) {
	    /* No output indicators were specified; these levels go to stderr. */
	    flags |= DUK_CONSOLE_STDERR_ONLY;
	}
	duk__console_reg_vararg_func(ctx, duk__console_warn, "warn", flags);
	duk__console_reg_vararg_func(ctx, duk__console_error, "error", flags);
	duk__console_reg_vararg_func(ctx, duk__console_error, "exception", flags);  /* alias to console.error */
	duk__console_reg_vararg_func(ctx, duk__console_dir, "dir", flags);

	duk_put_global_string(ctx, "console");

	/* Proxy wrapping: ensures any undefined console method calls are
	 * ignored silently.  This was required specifically by the
	 * DeveloperToolsWG proposal (and was implemented also by Firefox:
	 * https://bugzilla.mozilla.org/show_bug.cgi?id=629607).  This is
	 * apparently no longer the preferred way of implementing console.
	 * When Proxy is enabled, whitelist at least .toJSON() to avoid
	 * confusing JX serialization of the console object.
	 */

	if (flags & DUK_CONSOLE_PROXY_WRAPPER) {
		/* Tolerate failure to initialize Proxy wrapper in case
		 * Proxy support is disabled.
		 */
		(void) duk_peval_string_noresult(ctx,
			"(function(){"
			    "var D=function(){};"
			    "var W={toJSON:true};"  /* whitelisted */
			    "console=new Proxy(console,{"
			        "get:function(t,k){"
			            "var v=t[k];"
			            "return typeof v==='function'||W[k]?v:D;"
			        "}"
			    "});"
			"})();"
		);
	}
}