Skip to content

Commit

Permalink
done
Browse files Browse the repository at this point in the history
  • Loading branch information
V8 macOS Builder committed Feb 3, 2023
1 parent 5fa6663 commit 2be51e6
Show file tree
Hide file tree
Showing 26 changed files with 638 additions and 377 deletions.
133 changes: 133 additions & 0 deletions codegen/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package main

import (
"fmt"
"go/ast"
"go/parser"
"go/token"

"os"
"strings"
)

func main() {
// generate rtti
// generate typescript types
// generate adapters for different interfaces
// magic comments in the parser
var fileSet token.FileSet
if wd, err := os.Getwd(); err != nil {
panic(err)
} else if packages, err := parser.ParseDir(&fileSet, wd, nil, parser.ParseComments|parser.AllErrors); err != nil {
panic(err)
} else {
tags := []string{"js"}
for _, pkg := range packages {
for f, a := range pkg.Files {
if text, err := os.ReadFile(f); err != nil {
panic(err)
} else {
nodes := FindDeclarationCommentTags(string(text), tags, a)

for _, node := range nodes {
fmt.Println(node)
}
}

}
}
}
}

func FindDeclarationCommentTags(file string, tags []string, a *ast.File) []*TaggedNode {
decls := []*TaggedNode{}
for _, comments := range a.Comments {
declTags := []Tag{}
for _, comment := range comments.List {
for _, tag := range tags {
text := strings.TrimSpace(strings.TrimPrefix(comment.Text, "//"))
if strings.HasPrefix(text, tag+":") {
declTags = append(declTags, Tag{
Name: tag,
Text: strings.TrimSpace(strings.TrimPrefix(text, tag+":")),
})
}
}
}

if len(declTags) > 0 {
endPos := comments.End()
ast.Inspect(a, func(node ast.Node) bool {
if node == nil {
return false
}

if node.Pos() > endPos && strings.TrimSpace(file[endPos:node.Pos()]) == "" {
record := false
if _, ok := node.(*ast.StructType); ok {

record = true
} else if _, ok := node.(*ast.File); ok {
record = true
} else if _, ok := node.(*ast.FuncDecl); ok {
record = true
} else if _, ok := node.(*ast.Field); ok {
record = true
}

if record {
d := &TaggedNode{
Tags: declTags,
Node: node,
}
decls = append(decls, d)
return false
} else {
endPos = node.End()
}
}
return true
})
}

}

return decls
}

type Tag struct {
Name string
Text string
}

func (t *Tag) String() string {
return fmt.Sprintf("%s:%s", t.Name, t.Text)
}

type TaggedNode struct {
Tags []Tag
Node ast.Node
}

func (t *TaggedNode) String() string {
out := []string{}
for _, tag := range t.Tags {
out = append(out, tag.String())
}

node := ""

if v, ok := t.Node.(*ast.File); ok {
node = fmt.Sprintf("go package %s", v.Name)
} else if v, ok := t.Node.(*ast.TypeSpec); ok {
node = fmt.Sprintf("go struct %s", v)
} else if v, ok := t.Node.(*ast.Field); ok {
node = fmt.Sprintf("go field %s %s", v.Names[0], v.Type.(*ast.SelectorExpr).Sel)
} else if v, ok := t.Node.(*ast.FuncDecl); ok {
node = fmt.Sprintf("go func %s", v.Name)
} else {
node = fmt.Sprintf("%s", t.Node)
}

return fmt.Sprintf("%s\n%v", strings.Join(out, "\n"), node)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/grexie/isolates

go 1.17
go 1.18

require (
github.com/grexie/refutils v0.1.1
Expand Down
36 changes: 0 additions & 36 deletions install-v8.sh

This file was deleted.

28 changes: 28 additions & 0 deletions install/install-v8.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash -e

VERSION=$1
VERSION=${VERSION:-10.4.132.23}
SCHEME=$2
SCHEME=${SCHEME:-release}

DIR=/usr/local/include/v8
rm -Rf ${DIR}
mkdir -p ${DIR}
URL=https://github.com/grexie/v8-builder/releases/download/${VERSION}/v8-headers-${VERSION}.zip
cd ${DIR}
curl -sSL ${URL} -o v8-headers.zip
test -f v8-headers.zip && unzip v8-headers.zip 2>/dev/null >/dev/null && echo "Installed v8-headers-${VERSION}" || true
rm -f v8-headers.zip

for PLATFORM in macos linux android windows; do
for ARCH in arm64 x64 x86 arm; do
DIR=/usr/local/lib/v8/${ARCH}/${PLATFORM}
rm -Rf ${DIR}
mkdir -p ${DIR}
URL=https://github.com/grexie/v8-builder/releases/download/${VERSION}/v8-${PLATFORM}-${ARCH}-${SCHEME}-${VERSION}.zip
cd ${DIR}
curl -sSL ${URL} -o v8.zip
test -f v8.zip && unzip -j v8.zip 2>/dev/null >/dev/null && echo "Installed v8-${VERSION} for ${PLATFORM} ${ARCH} (${SCHEME})" || true
rm -f v8.zip
done
done
28 changes: 28 additions & 0 deletions install/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
_ "embed"
"io"
"log"
"os"
"os/exec"
)

//go:embed install-v8.sh
var script []byte

func main() {
args := append([]string{"-es", "-"}, os.Args[1:]...)
cmd := exec.Command("/bin/bash", args...)
stdout, _ := cmd.StdoutPipe()
stderr, _ := cmd.StderrPipe()
stdin, _ := cmd.StdinPipe()
cmd.Start()
go io.Copy(os.Stdout, stdout)
go io.Copy(os.Stderr, stderr)
stdin.Write(script)
stdin.Close()
if err := cmd.Wait(); err != nil {
log.Println(err)
}
}
8 changes: 6 additions & 2 deletions v8.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package isolates

// #include "v8_c_bridge.h"
// #cgo CXXFLAGS: -I${SRCDIR} -I${SRCDIR}/include -g3 -fno-rtti -fpic -std=c++11
// #cgo LDFLAGS: -pthread -L${SRCDIR}/libv8 -lv8_base -lv8_init -lv8_initializers -lv8_libbase -lv8_libplatform -lv8_libsampler -lv8_nosnapshot
// #cgo CXXFLAGS: -I/usr/local/include/v8 -g3 -fno-rtti -fpic -std=c++20
// #cgo LDFLAGS: -pthread -lv8_base_without_compiler -lv8_init -lv8_initializers -lv8_libbase -lv8_libplatform -lv8_snapshot
// #cgo darwin,arm64 LDFLAGS: -L/usr/local/lib/v8/arm64/macos
// #cgo darwin,amd64 LDFLAGS: -L/usr/local/lib/v8/x64/macos
// #cgo linux,arm64 LDFLAGS: -L/usr/local/lib/v8/arm64/linux
// #cgo linux,amd64 LDFLAGS: -L/usr/local/lib/v8/x64/linux
import "C"

import (
Expand Down
10 changes: 6 additions & 4 deletions v8_c_bridge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
#include <cstdlib>
#include <cstring>

extern "C" {
extern "C"
{
Version version = {V8_MAJOR_VERSION, V8_MINOR_VERSION, V8_BUILD_NUMBER, V8_PATCH_LEVEL};

void v8_Initialize() {
const char* flags = "--expose_gc";
void v8_Initialize()
{
const char *flags = "--expose_gc";
v8::V8::SetFlagsFromString(flags, strlen(flags));

platform = v8::platform::CreateDefaultPlatform();
platform = v8::platform::NewDefaultPlatform().get();
v8::V8::InitializePlatform(platform);
v8::V8::Initialize();
return;
Expand Down
3 changes: 1 addition & 2 deletions v8_c_bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ extern "C"
kDataView,
kSharedArrayBuffer,
kProxy,
kWebAssemblyCompiledModule,
kWasmModuleObject,
kNumKinds,
} Kind;

Expand Down Expand Up @@ -173,7 +173,6 @@ extern "C"
extern void v8_FunctionTemplate_Release(ContextPtr ctxptr, FunctionTemplatePtr fnptr);
extern void v8_FunctionTemplate_Inherit(ContextPtr ctxptr, FunctionTemplatePtr fnptr, FunctionTemplatePtr parentptr);
extern void v8_FunctionTemplate_SetName(ContextPtr pContext, FunctionTemplatePtr pFunction, const char *name);
extern void v8_FunctionTemplate_SetHiddenPrototype(ContextPtr ctxptr, FunctionTemplatePtr fnptr, bool value);
extern ValuePtr v8_FunctionTemplate_GetFunction(ContextPtr ctx, FunctionTemplatePtr fn);
extern ObjectTemplatePtr v8_FunctionTemplate_PrototypeTemplate(ContextPtr ctxptr, FunctionTemplatePtr function_ptr);
extern ObjectTemplatePtr v8_FunctionTemplate_InstanceTemplate(ContextPtr ctxptr, FunctionTemplatePtr function_ptr);
Expand Down
Loading

0 comments on commit 2be51e6

Please sign in to comment.