From c232cf149a7aa105398d7b76b035daa346c41c99 Mon Sep 17 00:00:00 2001 From: Vincent Sanders Date: Tue, 11 Aug 2015 10:30:47 +0100 Subject: Change to using single instance of constant strings for magic values Previously every reference to a constant magic string value was a separate and the compiler was not good at making them all common. This makes the string constants an explicit reference to a single instance of the string. --- src/duk-libdom.c | 78 ++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 51 insertions(+), 27 deletions(-) diff --git a/src/duk-libdom.c b/src/duk-libdom.c index 4eb0432..5f82bd1 100644 --- a/src/duk-libdom.c +++ b/src/duk-libdom.c @@ -27,13 +27,13 @@ #define MAGICPFX "\\xFF\\xFFNETSURF_DUKTAPE_" -#define NSGENBIND_PREAMBLE \ -"/* Generated by nsgenbind\n" \ -" *\n" \ -" * nsgenbind is published under the MIT Licence.\n" \ -" * nsgenbind is similar to a compiler is a purely transformative tool which\n"\ -" * explicitly makes no copyright claim on this generated output\n"\ -" */" +#define NSGENBIND_PREAMBLE \ + "/* Generated by nsgenbind\n" \ + " *\n" \ + " * nsgenbind is published under the MIT Licence.\n" \ + " * nsgenbind is similar to a compiler is a purely transformative tool which\n" \ + " * explicitly makes no copyright claim on this generated output\n" \ + " */" /** * Output code to create a private structure @@ -47,8 +47,8 @@ static int output_create_private(FILE* outf, char *class_name) fprintf(outf, "\tif (priv == NULL) return 0;\n"); fprintf(outf, "\tduk_push_pointer(ctx, priv);\n"); fprintf(outf, - "\tduk_put_prop_string(ctx, 0, \"%sPRIVATE\");\n\n", - MAGICPFX); + "\tduk_put_prop_string(ctx, 0, %s_magic_string_private);\n\n", + DLPFX); return 0; } @@ -58,12 +58,18 @@ static int output_create_private(FILE* outf, char *class_name) */ static int output_safe_get_private(FILE* outf, char *class_name, int idx) { - fprintf(outf, "\t%s_private_t *priv;\n", class_name); - fprintf(outf, "\tduk_get_prop_string(ctx, %d, \"%sPRIVATE\");\n", - idx, MAGICPFX); - fprintf(outf, "\tpriv = duk_get_pointer(ctx, -1);\n"); - fprintf(outf, "\tduk_pop(ctx);\n"); - fprintf(outf, "\tif (priv == NULL) return 0;\n\n"); + fprintf(outf, + "\t%s_private_t *priv;\n", class_name); + fprintf(outf, + "\tduk_get_prop_string(ctx, %d, %s_magic_string_private);\n", + idx, DLPFX); + fprintf(outf, + "\tpriv = duk_get_pointer(ctx, -1);\n"); + fprintf(outf, + "\tduk_pop(ctx);\n"); + fprintf(outf, + "\tif (priv == NULL) return 0;\n\n"); + return 0; } @@ -98,12 +104,16 @@ static int output_get_prototype(FILE* outf, const char *interface_name) proto_name = get_prototype_name(interface_name); - fprintf(outf, "\t/* get prototype */\n"); fprintf(outf, - "\tduk_get_global_string(ctx, \"%sPROTOTYPES\");\n", MAGICPFX); + "\t/* get prototype */\n"); + fprintf(outf, + "\tduk_get_global_string(ctx, %s_magic_string_prototypes);\n", + DLPFX); + fprintf(outf, + "\tduk_get_prop_string(ctx, -1, \"%s\");\n", + proto_name); fprintf(outf, - "\tduk_get_prop_string(ctx, -1, \"%s\");\n", proto_name); - fprintf(outf, "\tduk_replace(ctx, -2);\n"); + "\tduk_replace(ctx, -2);\n"); free(proto_name); @@ -246,8 +256,8 @@ output_get_method_private(FILE* outf, char *class_name) fprintf(outf, "\t/* Get private data for method */\n"); fprintf(outf, "\t%s_private_t *priv = NULL;\n", class_name); fprintf(outf, "\tduk_push_this(ctx);\n"); - fprintf(outf, "\tduk_get_prop_string(ctx, -1, \"%sPRIVATE\");\n", - MAGICPFX); + fprintf(outf, "\tduk_get_prop_string(ctx, -1, %s_magic_string_private);\n", + DLPFX); fprintf(outf, "\tpriv = duk_get_pointer(ctx, -1);\n"); fprintf(outf, "\tduk_pop_2(ctx);\n"); fprintf(outf, "\tif (priv == NULL) {\n"); @@ -1117,7 +1127,7 @@ output_interface_operation(FILE* outf, fprintf(outf, "if (%s_argc < %d) {\n" "\t\t/* not enough arguments */\n" - "\t\tduk_error(ctx, DUK_RET_TYPE_ERROR, %s_argument_error_fmt, %d, %s_argc);\n" + "\t\tduk_error(ctx, DUK_RET_TYPE_ERROR, %s_error_fmt_argument, %d, %s_argc);\n" "\t} else ", DLPFX, fixedargc, @@ -1604,9 +1614,14 @@ output_binding_header(struct interface_map *interface_map) "\n", MAGICPFX); + /* declaration of constant string values */ fprintf(bindf, - "extern const char *%s_argument_error_fmt;", - DLPFX); + "/* Constant strings */\n" + "extern const char *%s_error_fmt_argument;\n" + "extern const char *%s_magic_string_private;\n" + "extern const char *%s_magic_string_prototypes;\n" + "\n", + DLPFX, DLPFX, DLPFX); fprintf(bindf, "duk_bool_t %s_instanceof(duk_context *ctx, const char *klass);\n", @@ -1661,11 +1676,20 @@ output_binding_src(struct interface_map *interface_map) fprintf(bindf, "/* Error format strings */\n" - "const char *%s_argument_error_fmt =\"%%d argument required, but ony %%d present.\";\n", + "const char *%s_error_fmt_argument =\"%%d argument required, but ony %%d present.\";\n", DLPFX); fprintf(bindf, "\n"); + fprintf(bindf, + "/* Magic identifiers */\n" + "const char *%s_magic_string_private =\"%sPRIVATE\";\n" + "const char *%s_magic_string_prototypes =\"%sPROTOTYPES\";\n", + DLPFX, MAGICPFX, DLPFX, MAGICPFX); + + fprintf(bindf, "\n"); + + /* instance of helper */ fprintf(bindf, "duk_bool_t\n" @@ -1678,7 +1702,7 @@ output_binding_src(struct interface_map *interface_map) "\t\treturn false;\n" "\t}\n" "\t/* ... obj */\n" - "\tduk_get_global_string(ctx, \"%sPROTOTYPES\");\n" + "\tduk_get_global_string(ctx, %s_magic_string_prototypes);\n" "\t/* ... obj protos */\n" "\tduk_get_prop_string(ctx, -1, klass);\n" "\t/* ... obj protos goalproto */\n" @@ -1699,7 +1723,7 @@ output_binding_src(struct interface_map *interface_map) "\treturn false;\n" "}\n" "\n", - MAGICPFX); + DLPFX); /* prototype creation helper function */ fprintf(bindf, -- cgit v1.2.3