3
# Copyright 2011 the V8 project authors. All rights reserved.
4
# Redistribution and use in source and binary forms, with or without
5
# modification, are permitted provided that the following conditions are
8
# * Redistributions of source code must retain the above copyright
9
# notice, this list of conditions and the following disclaimer.
10
# * Redistributions in binary form must reproduce the above
11
# copyright notice, this list of conditions and the following
12
# disclaimer in the documentation and/or other materials provided
13
# with the distribution.
14
# * Neither the name of Google Inc. nor the names of its
15
# contributors may be used to endorse or promote products derived
16
# from this software without specific prior written permission.
18
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
# This is a utility for converting I18N JavaScript source code into C-style
31
# char arrays. It is used for embedded JavaScript code in the V8
33
# This is a pared down copy of v8/tools/js2c.py that avoids use of
34
# v8/src/natives.h and produces different cc template.
36
import os, re, sys, string
44
result.append(str(value))
46
return ", ".join(result)
49
def RemoveCommentsAndTrailingWhitespace(lines):
50
lines = re.sub(r'//.*\n', '\n', lines) # end-of-line comments
51
lines = re.sub(re.compile(r'/\*.*?\*/', re.DOTALL), '', lines) # comments.
52
lines = re.sub(r'\s+\n+', '\n', lines) # trailing whitespace
56
def ReadFile(filename):
57
file = open(filename, "rt")
65
EVAL_PATTERN = re.compile(r'\beval\s*\(');
66
WITH_PATTERN = re.compile(r'\bwith\s*\(');
69
def Validate(lines, file):
70
lines = RemoveCommentsAndTrailingWhitespace(lines)
71
# Because of simplified context setup, eval and with is not
72
# allowed in the natives files.
73
eval_match = EVAL_PATTERN.search(lines)
75
raise ("Eval disallowed in natives: %s" % file)
76
with_match = WITH_PATTERN.search(lines)
78
raise ("With statements disallowed in natives: %s" % file)
81
HEADER_TEMPLATE = """\
82
// Copyright 2011 Google Inc. All Rights Reserved.
84
// This file was generated from .js source files by gyp. If you
85
// want to make changes to this file you should either change the
86
// javascript source files or the i18n-js2c.py script.
88
#include "src/extensions/experimental/i18n-natives.h"
94
const char* I18Natives::GetScriptSource() {
95
// JavaScript source gets injected here.
96
static const char i18n_source[] = {%s};
106
def JS2C(source, target):
107
filename = str(source)
109
lines = ReadFile(filename)
110
Validate(lines, filename)
111
data = ToCArray(lines)
114
output = open(target, "w")
115
output.write(HEADER_TEMPLATE % data)
125
if __name__ == "__main__":