📄 jswrap.js
字号:
func.return_type = token;
token = get_token ();
}
else
func.return_type = tVOID;
/* The name of the function. */
if (token != tIDENTIFIER)
syntax_error ();
if (!valid_c_identifier_re.test (token_value))
{
System.error (input_file, ":", linenum,
": function name is not a valid C identifier `",
token_value, "'\n");
System.exit (1);
}
func.name = token_value;
/* Arguments. */
token = get_token ();
if (token != #'(')
syntax_error ();
var args = new Array ();
token = get_token ();
while (token != #')')
{
var arg = new Argument ();
while (token != tIDENTIFIER)
{
if (token == tEOF)
syntax_error ();
if (is_type_token (token))
arg.type = token;
else if (is_copy_type_token (token))
arg.copy_type = token;
else if (token == tSTATIC)
arg.staticp = true;
else
syntax_error ();
token = get_token ();
}
if (!valid_c_identifier_re.test (token_value))
{
System.error (input_file, ":", linenum,
": argument name is not a valid C identifier `",
token_value, "'\n");
System.exit (1);
}
arg.name = token_value;
/* Check some validity conditions. */
if (!arg.type)
{
System.error (input_file, ":", linenum,
": no type specified for argument `",
arg.name, "'\n");
System.exit (1);
}
if (arg.staticp)
{
if (arg.type != tCSTRING && arg.type != tSTRING)
{
System.error (input_file, ":", linenum,
": type `static' can only be used with `cstring' and `string' arguments\n");
System.exit (1);
}
if (arg.copy_type == tOUT)
System.error (input_file, ":", linenum,
": warning: type `static' is meaningful only with `in' and `inout' arguments\n");
}
args.push (arg);
token = get_token ();
if (token == #',')
token = get_token ();
}
func.args = args;
/* The opening '{' of the function body. */
token = get_token ();
if (token != #'{')
syntax_error ();
/* Get the function body. */
var code = new String ("");
while (true)
{
var line = ifp.readln ();
if (!line)
syntax_error ();
linenum++;
if (end_brace_re.test (line))
break;
code.append (line + "\n");
}
func.body = code;
func.generate_js ();
// func.print_js (ofp_js);
func.print_h (ofp_h, true);
func.print_c (ofp_c);
}
trailers ();
}
function get_token ()
{
var ch;
while ((ch = ifp.readByte ()) != -1)
{
if (ch == #' ' || ch == #'\t' || ch == #'\v' || ch == #'\r'
|| ch == #'\f')
continue;
if (ch == #'\n')
{
linenum++;
continue;
}
token_linenum = linenum;
if (ch == #'/' && peek_char () == #'*')
{
/* Multi line comment. */
ifp.readByte ();
while ((ch = ifp.readByte ()) != -1
&& (ch != #'*' || peek_char () != #'/'))
if (ch == #'\n')
linenum++;
/* Consume the peeked #'/' character. */
ifp.readByte ();
}
/* Identifiers and keywords. */
else if (JSC$lexer_is_identifier_letter (ch))
{
/* An identifier. */
var id = String.fromCharCode (ch);
while ((ch = ifp.readByte ()) != -1
&& (JSC$lexer_is_identifier_letter (ch)
|| JSC$lexer_is_decimal_digit (ch)))
id.append (File.byteToString (ch));
ifp.ungetByte (ch);
/* Keywords. */
if (id == "function")
return tFUNCTION;
/* Types. */
else if (id == "cstring")
return tCSTRING;
else if (id == "double")
return tDOUBLE;
else if (id == "int")
return tINT;
else if (id == "string")
return tSTRING;
else if (id == "void")
return tVOID;
else if (id == "in")
return tIN;
else if (id == "out")
return tOUT;
else if (id == "inout")
return tINOUT;
else if (id == "static")
return tSTATIC;
else
{
/* It really is an identifier. */
token_value = id;
return tIDENTIFIER;
}
}
/* Just return the character as-is. */
else
return ch;
}
/* EOF reached. */
return tEOF;
}
function peek_char ()
{
var ch = ifp.readByte ();
ifp.ungetByte (ch);
return ch;
}
function syntax_error ()
{
System.error (input_file, ":", linenum, ": syntax error\n");
System.exit (1);
}
function headers ()
{
var stream;
/* The header file. */
stream = ofp_h;
header_banner (stream);
var ppname = path_to_ppname (header_file);
stream.write ("\n#ifndef " + ppname
+ "\n#define " + ppname + "\n");
/* The C file. */
stream = ofp_c;
header_banner (stream);
stream.write ("\n#include <jsint.h>\n");
if (!opt_reentrant)
{
stream.write ("
/*
* This global interpreter handle can be removed with " + program + "'s
* option -r, --reentrant.
*/
");
stream.write ("extern JSInterpPtr jswrap_interp;\n");
}
if (opt_no_error_handler)
{
stream.write ("
/* Prototype for the error handler of the JS runtime errors. */
");
stream.write ("void jswrap_error (JSInterpPtr interp, char *error);\n");
}
else
{
/* Define the default jswrap_error() function. */
stream.write ("
/*
* This is the default error handler for the JS runtime errors. The default
* handler can be removed with " + program + "'s option -n, --no-error-handler.
* In this case, your code must define the handler function.
*/
");
stream.write ("\
static void
jswrap_error (JSInterpPtr interp, char *error)
{
const char *cp;
fprintf (stderr, \"JS runtime error: %s\", error);
cp = js_error_message (interp);
if (cp[0])
fprintf (stderr, \": %s\", cp);
fprintf (stderr, \"\\n\");
exit (1);
}
");
}
stream.write ("
/*
* The global function definitions.
*/
");
}
function header_banner (stream)
{
stream.write ("/* This file is automatically generated from `"
+ input_file + "' by " + program + ". */\n");
}
path_to_ppname_re = new RegExp ("[^A-Za-z_0-9]", "g");
function path_to_ppname (path)
{
var str = path.toUpperCase ().replace (path_to_ppname_re, "_");
if (#'0' <= str[0] && str[0] <= #'9')
str = "_" + str;
return str;
}
function trailers ()
{
/* The header file. */
var stream = ofp_h;
stream.write ("\n#endif /* not " + path_to_ppname (header_file) + " */\n");
}
/*
Local variables:
mode: c
End:
*/
/*
* JS C wrapper.
* Copyright (c) 1998 New Generation Software (NGS) Oy
*
* Author: Markku Rossi <mtr@ngs.fi>
*/
/*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
* MA 02111-1307, USA
*/
/*
* $Source: /cygdrive/c/RCVS/CVS/ReactOS/reactos/lib/kjs/jswrap/jswrap.js,v $
* $Id: jswrap.js 21681 2006-04-21 15:00:24Z peterw $
*/
/*
* Variables and definitions.
*/
version_number = "0.0.1";
/*
* Options.
*/
/*
* -g, --debug
*
* Generate debugging information to the generated JS byte-code.
*/
opt_debug = false;
/*
* -h FILE, --header FILE
*
* Generate the C header to file FILE.
*/
header_file = null;
/*
* -n, --no-error-handler
*
* Do not create the default error handler to the C files.
*/
opt_no_error_handler = false;
/*
* -o FILE, --output FILE
*
* Generate the C output to file FILE.
*/
output_file = null;
/*
* -r, --reentrant
*
* Generate re-entrant C stubs.
*/
opt_reentrant = false;
/*
* -V, --version
*
* Print version information and exit successfully.
*/
/*
* --help
*
* Print short help and exit successfully.
*/
/*
* Functions.
*/
function main ()
{
var idx = ARGS[0].lastIndexOf ("/");
if (idx >= 0)
program = ARGS[0].substr (idx + 1);
else
program = ARGS[0];
/* Handle arguments. */
var i;
for (i = 1; i < ARGS.length; i++)
{
if (ARGS[i][0] == #'-')
{
if (ARGS[i] == "-g" || ARGS[i] == "--debug")
opt_debug = true;
else if (ARGS[i] == "-h" || ARGS[i] == "--header")
{
if (i + 1 >= ARGS.length)
{
System.error (program,
": no argument for option --header\n");
System.exit (1);
}
header_file = ARGS[++i];
}
else if (ARGS[i] == "-n" || ARGS[i] == "--no-error-handler")
opt_no_error_handler = true;
else if (ARGS[i] == "-o" || ARGS[i] == "--output")
{
if (i + 1 >= ARGS.length)
{
System.error (program,
": no argument for option --output\n");
System.exit (1);
}
output_file = ARGS[++i];
}
else if (ARGS[i] == "-r" || ARGS[i] == "--reentrant")
opt_reentrant = true;
else if (ARGS[i] == "-V" || ARGS[i] == "--version")
{
version ();
System.exit (0);
}
else if (ARGS[i] == "--help")
{
usage ();
System.exit (0);
}
else
{
/* Unrecognized option. */
System.error (program, ": unrecognized option `",
ARGS[i], "'\n");
System.error ("Try `", program,
" --help' for more information.\n");
System.exit (1);
}
}
else
{
/* End of arguments. */
break;
}
}
if (i >= ARGS.length)
{
System.error (program, ": no input file specified\n");
System.exit (1);
}
if (i + 1 < ARGS.length)
{
System.error (program, ": extraneous arguments after input file\n");
System.exit (1);
}
input_file = ARGS[i];
/* Set the defaults. */
var re = new RegExp ("^(.*)\\.jsw$");
if (!header_file)
{
if (re.test (input_file))
header_file = RegExp.$1 + ".h";
else
header_file = input_file + ".h";
}
if (!output_file)
{
if (re.test (input_file))
output_file = RegExp.$1 + ".c";
else
output_file = input_file + ".c";
}
if (false)
{
if (re.test (input_file))
js_file = RegExp.$1 + ".js";
else
js_file = input_file + ".js";
}
process ();
}
function usage ()
{
System.print ("\
Usage: ", program, " [OPTION]... FILE\n\
Mandatory arguments to long options are mandatory for short options too.\n");
System.print ("\
-g, --debug generate debugging information to the generated\n\
JS byte-code\n\
-h, --header FILE generate the C header to file FILE\n\
-n, --no-error-handler do not generate the default error handler\n\
-o, --output FILE generate the C output to file FILE\n\
-r, --reentrant generate re-entrant C stubs\n\
-V, --version print version number\n\
--help print this help and exit\n\
");
System.print ("\nReport bugs to mtr@ngs.fi.\n");
}
function version ()
{
System.print ("NGS JavaScript C wrapper generator ", version_number, "\n");
System.print ("\
Copyright (C) 1998 New Generation Software (NGS) Oy.\n\
NGS JavaScript Interpreter comes with NO WARRANTY, to the extent\n\
permitted by law. You may redistribute copies of NGS JavaScript\n\
Interpreter under the terms of the GNU Library General Public License.\n\
For more information about these matters, see the files named COPYING.\n\
");
}
main ();
/*
Local variables:
mode: c
End:
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -