def change_id(node, types, symbols, cand0): for tys in [types, [JSType.js_object]]: for syms in [cand0, symbols, (builtin.SYMS, [])]: if len(syms) == 0: continue cand = find_cand(tys, syms) if len(cand) > 0: nxt = pick_one(cand) if 'name' in node and hasattr(nxt, 'symbol'): node['name'] = nxt.symbol return nxt error('change_id fail')
def resolve_FuncCall(node, parent, symbols, is_global, is_check, cand, hlist): global_var, local_var = symbols go_flag = True callee_type = get_node_type(node['callee']) if callee_type == 'Identifier': name = node['callee']['name'] if (name in builtin.FUNCS or name in builtin.OBJS or name in builtin.ARRAYS): expr = None elif name in ID_HARNESS_MAP: if not is_duplicate(hlist, name): fname = pick_one(ID_HARNESS_MAP[name]) hlist.append(fname) expr = None else: symbol = find_symbol(node['callee'], symbols) if symbol == None: symbol = change_id(node['callee'], [JSType.js_func], symbols, cand) expr = symbol.expr go_flag = symbol.get_flag() symbol.set_flag(False) elif callee_type in ['FunctionExpression', 'ArrowFunctionExpression']: expr = node['callee'] elif callee_type in ['MemberExpression', 'CallExpression', 'SequenceExpression']: resolve_id(node['callee'], node, symbols, is_global, is_check, cand, hlist) expr = None elif callee_type == 'NewExpression': node['callee']['callee']['name'] = 'Function' return symbols elif callee_type == 'BlockStatement': resolve_list(node['body'], node, symbols, is_global, is_check, cand, hlist) expr = None else: error('resolve_id FunctionCall fail') expr = None resolve_list(node['arguments'], node, symbols, is_global, is_check, cand, hlist) if (go_flag and expr != None and 'params' in expr and 'body' in expr): l1 = [] for arg in expr['params']: if get_node_type(arg) == 'Identifier': l1.append(Symbol(arg, arg)) l1.append(Symbol('arguments', None, JSType.js_array)) symbols = global_var, l1 symbols = hoisting(expr['body'], symbols, False) resolve_id(expr['body'], node, symbols, False, False, cand, hlist) return global_var, local_var
def __init__(self, identifier, expr, ty=None): if ('type' in identifier and identifier['type'] == 'Identifier'): self.symbol = identifier['name'] else: self.symbol = identifier if type(self.symbol) not in [str, bytes]: error('self.symbol is not string') if ty == None: self.ty = to_jstype(expr) else: self.ty = ty self.expr = expr self.flag = True
def help_Assign(pattern, parent, init, symbols, is_global, is_VarDecl, is_check, cand, hlist): if pattern == None: return symbols pattern_type = get_node_type(pattern) if pattern_type == 'Identifier': if is_check: return symbols ty = get_type(init, symbols) if is_VarDecl: sym = find_symbol(pattern, symbols) if sym == None: error('help_VarDecl fail') sym.update_type(ty) else: sym = Symbol(pattern, None, ty) symbols[0].append(sym) return symbols elif pattern_type == 'ArrayPattern': items = pattern['elements'] for idx in range(len(items)): item = items[idx] item_init = get_Array_item(init, idx) symbols = help_Assign(item, pattern, item_init, symbols, is_global, is_VarDecl, is_check, cand, hlist) return symbols elif pattern_type == 'ObjectPattern': for prop in pattern['properties']: prop_init = get_Object_prop(init, prop['key']) symbols = help_Assign(prop['value'], pattern, prop_init, symbols, is_global, is_VarDecl, is_check, cand, hlist) return symbols elif pattern_type == 'MemberExpression': return resolve_id(pattern, parent, symbols, is_global, is_check, cand, hlist) elif pattern_type == 'AssignmentPattern': # TODO: Check return symbols else: error('Unknown branch in help assign') return symbols
def pattern_hoisting(pattern, node): if pattern == None: return [] pattern_type = get_node_type(pattern) if pattern_type == 'Identifier': return [Symbol(pattern, node, JSType.undefined)] elif pattern_type == 'ArrayPattern': ret = [] for item in pattern['elements']: ret += pattern_hoisting(item, pattern) return ret elif pattern_type == 'ObjectPattern': ret = [] for prop in pattern['properties']: ret += pattern_hoisting(prop, pattern) return ret elif pattern_type == 'AssignmentPattern': return pattern_hoisting(pattern['left'], pattern) elif pattern_type == 'Property': return pattern_hoisting(pattern['value'], pattern) elif pattern_type == 'RestElement': return pattern_hoisting(pattern['argument'], pattern) else: error('pattern_hoisting: %s %s' % (pattern['type'], node['type'])) return []