Skip to content

Commit 4db23af

Browse files
committed
fix: java run result
1 parent 827de41 commit 4db23af

File tree

8 files changed

+46
-36
lines changed

8 files changed

+46
-36
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ It's still under development. Here's how it works:
2626
$ npm install -g @dartnative/codegen
2727
```
2828

29+
## Requirement
30+
31+
``` json
32+
"node": ">=12.16"
33+
```
34+
2935
## Usage
3036

3137
```

bin/codegen.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
const { program } = require('commander')
33
const main = require('../lib/main').main
44

5-
program.version('1.1.1')
5+
program.version('1.1.5')
66

77
program
88
.arguments('<input>', 'Iutput directory')

index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
const main = require('./lib/main').main
22

3-
let dataPath = "test/objc/RuntimeStub.h"
3+
let path = 'test/objc/RuntimeStub.h'
4+
// let path = 'test/java/helloworld.java'
5+
46
let option = {
57
output: 'test/dart',
6-
template: 'plugin',
7-
projectName: 'testPlugin'
8+
// template: 'plugin',
9+
// projectName: 'testPlugin'
810
}
911

10-
main(dataPath, option, true)
12+
main(path, option, true)

lib/java/DNJavaConverter.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
var antlr4 = require('antlr4')
2-
var rf = require("fs")
32
var JavaLexer = require('../../parser/java/Java9Lexer').Java9Lexer
43
var JavaParser = require('../../parser/java/Java9Parser').Java9Parser
54
var DNJavaParserListener = require('./DNJavaParserListener').DNJavaParserListener
@@ -13,39 +12,41 @@ try {
1312
console.log('NodeJS version is too low. Running on slow mode.')
1413
}
1514

16-
function convert(path, cb) {
17-
if (!path) {
15+
function convert(content, cb) {
16+
if (!content) {
1817
if (!workerData) {
19-
console.log('return: ')
18+
if (cb) {
19+
cb(null, null)
20+
}
2021
return
2122
}
22-
path = workerData.path
23+
content = workerData.content
2324
}
2425
if (!cb) {
2526
cb = callback
2627
}
27-
28+
2829
try {
29-
var content = rf.readFileSync(path, "utf-8")
3030
var chars = new antlr4.InputStream(content)
3131
var lexer = new JavaLexer(chars)
3232
lexer.addErrorListener(new ConsoleErrorListener())
3333

3434
var tokens = new antlr4.CommonTokenStream(lexer)
3535
var parser = new JavaParser(tokens)
3636
parser.addErrorListener(new ConsoleErrorListener())
37-
38-
var listener = new DNJavaParserListener(cb, path)
37+
38+
let isPartOfFile = workerData ? workerData.isPartOfFile : false
39+
var listener = new DNJavaParserListener(cb, isPartOfFile)
3940
var tree = parser.compilationUnit()
4041
antlr4.tree.ParseTreeWalker.DEFAULT.walk(listener, tree)
4142
} catch (e) {
42-
cb(null, path, e)
43+
cb(null, e)
4344
}
4445
}
4546

4647
function callback(result, path, error) {
4748
// Send a message to the main thread.
48-
parentPort.postMessage({result: result, path: path, error: error});
49+
parentPort.postMessage({ result: result, error: error });
4950
}
5051

5152
convert()

lib/java/DNJavaParserListener.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,16 @@ var DNRootContext = c.DNRootContext,
99
DNArgumentContext = c.DNArgumentContext
1010

1111
class DNJavaParserListener extends JavaListener {
12-
constructor(cb, path) {
12+
constructor(cb, isPartOfFile) {
1313
super()
14-
JavaListener.call(this)
1514
//success callack
1615
this.cb = cb
17-
this.path = path
16+
this.isPartOfFile = isPartOfFile
1817
}
1918

2019
buildDart() {
2120
var dart = this.rootContext.parse()
22-
this.cb(dart,this.path,null)
21+
this.cb(dart, null)
2322
}
2423

2524
// Enter a parse tree produced by Java9Parser#literal.

lib/main.js

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const { execSync } = require('child_process')
22
const fs = require("fs")
3-
let rf = require("fs")
43
const path = require("path")
54
const yaml = require('js-yaml')
65
let Worker
@@ -29,7 +28,8 @@ const scriptForExtension = {
2928
const fileSplitConfig = new Map()
3029
fileSplitConfig.set(scriptForExtension.h, {
3130
separator: '@end\n',
32-
maxLength: 5000}
31+
maxLength: 5000
32+
}
3333
)
3434

3535
function mkdirs(dirname) {
@@ -80,9 +80,9 @@ function writeOutputToFileByPath(result, srcPath) {
8080
components = srcFile.split('.').slice(0, -1)
8181
components.push('dart')
8282
}
83-
83+
8484
let dartFile = components.join('.').toLowerCase()
85-
85+
8686
var outputFile = outputDir ? path.join(outputDir, dartFile) : dartFile
8787
fs.writeFileSync(outputFile, result)
8888
return outputFile
@@ -114,7 +114,7 @@ function formatDartFile(dartPath) {
114114
function createFlutterPackage(template, projectName) {
115115
var command = 'flutter create'
116116
if (template == 'plugin') {
117-
command += '--platforms=android,ios --template=' + template
117+
command += ' --platforms=android,ios --template=' + template
118118
}
119119
command += ' --project-name ' + projectName + ' ' + outputDir
120120
execSync(command, { stdio: 'inherit' })
@@ -140,7 +140,7 @@ function generateDartWithWorker(content, path, script) {
140140
resolve({ result: result, error: error })
141141
})
142142
} else {
143-
isPartOfFile = path.endsWith(dnPartExtension) && !path.endsWith(`0.${dnPartExtension}`)
143+
const isPartOfFile = path.endsWith(dnPartExtension) && !path.endsWith(`0.${dnPartExtension}`)
144144
const worker = new Worker(script, {
145145
workerData: { content: content, isPartOfFile: isPartOfFile },
146146
resourceLimits: { maxOldGenerationSizeMb: 8 * 1024 }
@@ -166,15 +166,18 @@ function generateDartWithWorker(content, path, script) {
166166
})
167167
}
168168

169-
function splitCodeFileIfNeed(separator, maxLength, path) {
170-
const content = rf.readFileSync(path, "utf-8")
171-
if (!this.isMainThread && isSupportWorkerThreads && content.length > maxLength) {
172-
let components = content.split(separator)
169+
function splitCodeFileIfNeed(config, path) {
170+
const content = fs.readFileSync(path, "utf-8")
171+
if (!config || !config.separator || !config.maxLength) {
172+
return [content]
173+
}
174+
if (!this.isMainThread && isSupportWorkerThreads && content.length > config.maxLength) {
175+
let components = content.split(config.separator)
173176
return components.map((c, i) => {
174177
if (i == components.length - 1) {
175178
return c
176179
}
177-
return c + separator
180+
return c + config.separator
178181
})
179182
}
180183
return [content]
@@ -185,7 +188,7 @@ async function runWorkItems(workItems) {
185188
let script = workItems.get(filePath)
186189
let config = fileSplitConfig.get(script)
187190
console.log('processing: ' + filePath)
188-
let contents = splitCodeFileIfNeed(config.separator, config.maxLength, filePath)
191+
let contents = splitCodeFileIfNeed(config, filePath)
189192
let ps = contents.map((content, index) => {
190193
let p = filePath
191194
if (contents.length > 1) {
@@ -208,7 +211,7 @@ function checkTemplateValid(template) {
208211
async function main(input, options, onMainThread = !isSupportWorkerThreads) {
209212
this.isMainThread = onMainThread
210213

211-
language = options.language
214+
var language = options.language
212215
if (!language) {
213216
language = 'auto'
214217
}
@@ -228,7 +231,7 @@ async function main(input, options, onMainThread = !isSupportWorkerThreads) {
228231
mkdirs(outputDir)
229232

230233
projectName = options.projectName
231-
template = options.template
234+
const template = options.template
232235
if (projectName && checkTemplateValid(template)) {
233236
outputDir = path.join(outputDir, projectName)
234237
createFlutterPackage(template, projectName)

lib/objc/DNObjectiveCParserListener.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ let DNRootContext = c.DNRootContext,
2222
class DNObjectiveCParserListener extends ObjectiveCParserListener {
2323
constructor(cb, isPartOfFile) {
2424
super()
25-
ObjectiveCParserListener.call(this)
2625
//success callack
2726
this.cb = cb
2827
this.isPartOfFile = isPartOfFile

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)