Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python内置模块 #8

Open
AlexZ33 opened this issue Mar 2, 2020 · 0 comments
Open

Python内置模块 #8

AlexZ33 opened this issue Mar 2, 2020 · 0 comments

Comments

@AlexZ33
Copy link
Member

AlexZ33 commented Mar 2, 2020

The Python Standard Library

argparse

在看v8引擎的源码时候 看了一些python脚本工具,
如 https://github.com/v8/v8/blob/master/tools/wasm-compilation-hints/inject-compilation-hints.py
image

image

#!/usr/bin/env python

# Copyright 2019 the V8 project authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be found
# in the LICENSE file.
# 导入模块
import argparse
import io
import sys

from wasm import *

FUNCTION_SECTION_ID = 3

def parse_args():
  # 创建一个解析对象
  # description 参数可以用于插入描述脚本用途的信息,可以为空于
  parser = argparse.ArgumentParser(\
      description="Inject compilation hints into a Wasm module.")
  # 向该对象中添加你要关注的命令行参数和选项
  parser.add_argument("-i", "--in-wasm-file", \
      type=str, \
      help="original wasm module")
  parser.add_argument("-o", "--out-wasm-file", \
      type=str, \
      help="wasm module with injected hints")
  parser.add_argument("-x", "--hints-file", \
      type=str, required=True, \
      help="binary hints file to be injected as a custom section " + \
          "'compilationHints'")
  # 进行解析
  return parser.parse_args()

if __name__ == "__main__":
  args = parse_args()
  in_wasm_file = args.in_wasm_file if args.in_wasm_file else sys.stdin.fileno()
  out_wasm_file = args.out_wasm_file if args.out_wasm_file else sys.stdout.fileno()
  hints_bs = open(args.hints_file, "rb").read()
  with io.open(in_wasm_file, "rb") as fin:
    with io.open(out_wasm_file, "wb") as fout:
      magic_number, bs = read_magic_number(fin);
      fout.write(bs)
      version, bs = read_version(fin);
      fout.write(bs)
      num_declared_functions = None
      while True:
        id, bs = read_varuintN(fin)
        fout.write(bs)
        if id == None:
          break
        payload_length, bs = read_varuintN(fin)
        fout.write(bs)

        # Peek into function section for upcoming validity check.
        if id == FUNCTION_SECTION_ID:
          num_declared_functions, bs = peek_varuintN(fin)

        bs = fin.read(payload_length)
        fout.write(bs)

        # Instert hint section after function section.
        if id == FUNCTION_SECTION_ID:
          assert len(hints_bs) == num_declared_functions, "unexpected number of hints"
          write_compilation_hints_section(fout, hints_bs)

参考

https://www.jianshu.com/p/a41fbd4919f8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant