Skip to content

Commit 842035b

Browse files
committed
First commit
0 parents  commit 842035b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+9784
-0
lines changed

.devcontainer/Dockerfile

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.241.1/containers/ubuntu/.devcontainer/base.Dockerfile
2+
3+
# [Choice] Ubuntu version (use ubuntu-22.04 or ubuntu-18.04 on local arm64/Apple Silicon): ubuntu-22.04, ubuntu-20.04, ubuntu-18.04
4+
ARG VARIANT="jammy"
5+
FROM mcr.microsoft.com/vscode/devcontainers/base:0-${VARIANT}
6+
7+
RUN sed -i 's/ports.ubuntu.com/mirrors.ustc.edu.cn/' /etc/apt/sources.list
8+
9+
RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
10+
&& apt-get -y install --no-install-recommends cmake python3 xz-utils bzip2 python-is-python3 make
11+
12+
COPY install-emscripten.sh .

.devcontainer/devcontainer.json

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
2+
// https://github.com/microsoft/vscode-dev-containers/tree/v0.241.1/containers/ubuntu
3+
{
4+
"name": "Ubuntu",
5+
"build": {
6+
"dockerfile": "Dockerfile",
7+
// Update 'VARIANT' to pick an Ubuntu version: jammy / ubuntu-22.04, focal / ubuntu-20.04, bionic /ubuntu-18.04
8+
// Use ubuntu-22.04 or ubuntu-18.04 on local arm64/Apple Silicon.
9+
"args": { "VARIANT": "ubuntu-22.04" }
10+
},
11+
12+
// Use 'forwardPorts' to make a list of ports inside the container available locally.
13+
// "forwardPorts": [],
14+
15+
// Use 'postCreateCommand' to run commands after the container is created.
16+
// "postCreateCommand": "uname -a",
17+
18+
// Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
19+
"remoteUser": "vscode",
20+
"features": {
21+
"git": "os-provided",
22+
"node": "18"
23+
},
24+
"settings": {
25+
"terminal.integrated.defaultProfile.linux": "zsh"
26+
}
27+
}

.devcontainer/install-emscripten.sh

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bash
2+
3+
VERSION=latest
4+
5+
cd
6+
git clone https://github.com/emscripten-core/emsdk.git --depth 1
7+
cd emsdk
8+
./emsdk install $VERSION
9+
./emsdk activate $VERSION

.eslintrc.cjs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module.exports = {
2+
root: true,
3+
env: {
4+
browser: true,
5+
es2021: true,
6+
node: true,
7+
},
8+
extends: [
9+
'airbnb-base',
10+
'airbnb-typescript/base',
11+
'plugin:@typescript-eslint/recommended',
12+
],
13+
parser: '@typescript-eslint/parser',
14+
parserOptions: {
15+
ecmaVersion: 'latest',
16+
sourceType: 'module',
17+
project: ['./tsconfig.eslint.json'],
18+
},
19+
plugins: [
20+
'@typescript-eslint',
21+
],
22+
ignorePatterns: [
23+
'generated/',
24+
'native/',
25+
'dist/',
26+
],
27+
rules: {
28+
'no-unreachable': 'warn',
29+
'no-constant-condition': ['error', { checkLoops: false }],
30+
'no-plusplus': ['error', { allowForLoopAfterthoughts: true }],
31+
'no-spaced-func': 'off',
32+
'@typescript-eslint/no-non-null-assertion': 'off',
33+
'@typescript-eslint/no-explicit-any': 'off',
34+
'@typescript-eslint/semi': ['warn', 'never'],
35+
},
36+
}

.gitignore

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
node_modules/
2+
.vscode/
3+
dist/
4+
generated/
5+
6+
.pnp.*
7+
.yarn/*
8+
!.yarn/patches
9+
!.yarn/plugins
10+
!.yarn/releases
11+
!.yarn/sdks
12+
!.yarn/versions

.gitmodules

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[submodule "packages/capstone/native"]
2+
path = packages/capstone/native
3+
url = https://github.com/capstone-engine/capstone.git
4+
shallow = true
5+
[submodule "packages/keystone/native"]
6+
path = packages/keystone/native
7+
url = https://github.com/keystone-engine/keystone.git
8+
shallow = true

.yarn/releases/yarn-3.2.1.cjs

+786
Large diffs are not rendered by default.

.yarnrc.yml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
nodeLinker: node-modules
2+
enableTransparentWorkspaces: false
3+
4+
yarnPath: .yarn/releases/yarn-3.2.1.cjs

capstone.code-workspace

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"folders": [
3+
{
4+
"name": "root",
5+
"path": "."
6+
},
7+
{
8+
"path": "packages/capstone"
9+
},
10+
{
11+
"path": "packages/keystone"
12+
},
13+
{
14+
"path": "playground"
15+
}
16+
],
17+
"settings": {
18+
"typescript.tsdk": "node_modules/typescript/lib"
19+
}
20+
}

package.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"private": true,
3+
"name": "capstone",
4+
"workspaces": [
5+
"packages/*",
6+
"playground"
7+
],
8+
"devDependencies": {
9+
"@typescript-eslint/eslint-plugin": "^5.30.6",
10+
"@typescript-eslint/parser": "^5.30.6",
11+
"eslint": "^8.19.0",
12+
"eslint-config-airbnb-base": "^15.0.0",
13+
"eslint-config-airbnb-typescript": "^17.0.0",
14+
"eslint-plugin-import": "^2.26.0"
15+
},
16+
"packageManager": "[email protected]"
17+
}

packages/capstone/build-lib.sh

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/usr/bin/env bash
2+
3+
set -eu
4+
5+
EMSDK=~/emsdk
6+
EMSCRIPTEN=$EMSDK/upstream/emscripten
7+
GENERATED_DIR=$(realpath src/generated)
8+
OUTPUT_NAME=capstone
9+
10+
ARCHS=(
11+
ARM
12+
ARM64
13+
X86
14+
)
15+
16+
BUILD_FLAGS=(
17+
-D CAPSTONE_BUILD_TESTS=OFF
18+
-D CAPSTONE_BUILD_CSTOOL=OFF
19+
20+
-D CAPSTONE_ARCHITECTURE_DEFAULT=OFF
21+
-D CAPSTONE_INSTALL=OFF
22+
)
23+
24+
EXPORTED_FUNCTIONS=(
25+
free
26+
malloc
27+
28+
cs_open
29+
cs_disasm
30+
cs_malloc
31+
cs_free
32+
cs_close
33+
cs_option
34+
cs_reg_name
35+
cs_op_count
36+
cs_op_index
37+
cs_insn_name
38+
cs_group_name
39+
cs_insn_group
40+
cs_reg_read
41+
cs_reg_write
42+
cs_errno
43+
cs_version
44+
cs_support
45+
cs_strerror
46+
cs_regs_access
47+
)
48+
EXPORTED_FUNCTIONS=$(echo -n "${EXPORTED_FUNCTIONS[*]}" | jq -cR 'split(" ") | map("_" + .)')
49+
50+
EMSCRIPTEN_SETTINGS=(
51+
-s MODULARIZE
52+
-s EXPORT_ES6
53+
# -s WASM_BIGINT
54+
-s EXPORTED_FUNCTIONS=$EXPORTED_FUNCTIONS
55+
-s EXPORTED_RUNTIME_METHODS=ccall,cwrap,getValue,UTF8ToString
56+
-s EXPORT_NAME=$OUTPUT_NAME
57+
-s ENVIRONMENT=web,node
58+
# -s ASSERTIONS
59+
)
60+
61+
for ARCH in "${ARCHS[@]}"; do
62+
CAPSTONE_BUILD_FLAGS+=(
63+
-DCAPSTONE_${ARCH}_SUPPORT=ON
64+
)
65+
done
66+
67+
cd native
68+
$EMSCRIPTEN/emcmake cmake -B build ${BUILD_FLAGS[*]} -DCMAKE_BUILD_TYPE=Release
69+
70+
cd build
71+
cmake --build . -j
72+
73+
$EMSCRIPTEN/emcc lib$OUTPUT_NAME.a -Os --minify 0 ${EMSCRIPTEN_SETTINGS[*]} -o $OUTPUT_NAME.mjs
74+
cp $OUTPUT_NAME.mjs $OUTPUT_NAME.wasm $GENERATED_DIR
75+
76+
cd ../..
77+
./generator.py const > "$GENERATED_DIR/const.ts"

packages/capstone/generator.py

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/usr/bin/env python3
2+
3+
import re
4+
import sys
5+
from typing import Generator
6+
7+
BINDING_DIR = 'native/bindings/java/capstone'
8+
ENTRYPOINT_FILE = f'{BINDING_DIR}/Capstone.java'
9+
10+
def get_const(start_markup, end_markup) -> Generator[None, str, list]:
11+
re_const = re.compile(r'(\w+\s+= .+);')
12+
re_space = re.compile(r'\s+')
13+
14+
while True:
15+
line = yield
16+
17+
if start_markup in line:
18+
break
19+
20+
result = []
21+
while True:
22+
line = yield
23+
24+
if not line:
25+
result.append(None)
26+
27+
if end_markup in line:
28+
break
29+
30+
if not line.startswith('public'):
31+
continue
32+
33+
match = re_const.search(line)
34+
if not match:
35+
print("Warning: No const found for '{}'".format(line), file=sys.stderr)
36+
continue
37+
38+
result.append(re_space.sub(' ', match.group(1)))
39+
40+
return result
41+
42+
def process_file(file_path, visitor):
43+
with open(file_path) as f:
44+
next(visitor)
45+
46+
for line in f:
47+
try:
48+
visitor.send(line.strip())
49+
except StopIteration as e:
50+
return e.value
51+
52+
def handle_get_const():
53+
def _c(line):
54+
if not line:
55+
return ''
56+
57+
return f'export const {line}'
58+
59+
result = process_file(ENTRYPOINT_FILE, get_const('Capstone API version', 'NativeStruct'))
60+
result = [
61+
'// AUTO-GENERATED FILE, DO NOT EDIT',
62+
'/* eslint-disable */',
63+
'',
64+
*map(_c, result),
65+
]
66+
67+
return result
68+
69+
def main():
70+
if len(sys.argv) == 1:
71+
print('Missing action')
72+
exit(1)
73+
74+
action = sys.argv[1]
75+
76+
if action == "const":
77+
result = handle_get_const()
78+
else:
79+
print('Invalid action')
80+
exit(1)
81+
82+
print(*result, sep='\n')
83+
84+
if __name__ == '__main__':
85+
main()

packages/capstone/native

Submodule native added at d5141c0

packages/capstone/package.json

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "capstone-wasm",
3+
"version": "1.0.0",
4+
"type": "module",
5+
"main": "./dist/capstone-wasm.umd.cjs",
6+
"module": "dist/capstone-wasm.js",
7+
"types": "./dist/index.d.ts",
8+
"exports": {
9+
".": {
10+
"import": "./dist/capstone-wasm.js",
11+
"require": "./dist/capstone-wasm.umd.cjs"
12+
}
13+
},
14+
"scripts": {
15+
"build": "vite build && yarn build:types",
16+
"build:types": "tsup src --dts src/index.ts --dts-only",
17+
"lint": "eslint .",
18+
"dev": "tsc -w",
19+
"test": "vitest"
20+
},
21+
"devDependencies": {
22+
"eslint": "^8.19.0",
23+
"tsup": "^6.1.3",
24+
"typescript": "^4.7.4",
25+
"vite": "^3.0.0",
26+
"vitest": "^0.18.0"
27+
},
28+
"files": [
29+
"dist"
30+
]
31+
}

packages/capstone/src/generated/.gitkeep

Whitespace-only changes.

packages/capstone/src/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export { default as loadCapstone, Capstone } from './wrapper'
2+
export type { Insn } from './wrapper'
3+
export * as Const from './generated/const'
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare module 'capstone' {
2+
export default function createCapstone(): Promise
3+
}

0 commit comments

Comments
 (0)