Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Saiv46 committed Jun 13, 2020
2 parents 1c5d3d1 + 806878f commit a8419b9
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 76 deletions.
4 changes: 0 additions & 4 deletions doc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,6 @@ The `path` is an array of namespace keys which select a path of namespaces to be

Compile and return a `ProtoDef` object, optionaly print the generated javascript code.

### ProtoDefCompiler.compileProtoDef(options = { optimize: false, printCode: false, printOptimizedCode: false })

Async function, returns a promise. Compile and return a `ProtoDef` object, optionaly print the generated javascript code. When `optimize = true`, use closure-compiler to optimize the generated code.

## utils

Some functions that can be useful to build new datatypes reader and writer.
Expand Down
4 changes: 4 additions & 0 deletions doc/history.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# History

## 1.7.2

* remove closure compiler

## 1.7.1

* fix option in compiler
Expand Down
11 changes: 0 additions & 11 deletions examples/compiled.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,4 @@ const packetData = {
time = performance.now() - start
ps = nbTests / 10 / time
console.log('read / write parser: ' + time.toFixed(2) + ' ms (' + ps.toFixed(2) + 'k packet/s)')

// Closure optimized:
const optimizedProto = await compiler.compileProtoDef({ optimize: true })
start = performance.now()
for (let i = 0; i < nbTests; i++) {
const result = optimizedProto.parsePacketBuffer(mainType, buffer).data
optimizedProto.createPacketBuffer(mainType, result)
}
time = performance.now() - start
ps = nbTests / time
console.log('read / write compiled (+closure): ' + time.toFixed(2) + ' ms (' + ps.toFixed(2) + 'k packet/s)')
})()
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "protodef",
"version": "1.7.1",
"version": "1.7.2",
"description": "A simple yet powerful way to define binary protocols",
"main": "index.js",
"author": "roblabla <[email protected]>",
Expand All @@ -16,7 +16,6 @@
"tonicExampleFilename": "example.js",
"license": "MIT",
"dependencies": {
"google-closure-compiler": "^20200504.0.0",
"lodash.get": "^4.4.2",
"lodash.reduce": "^4.6.0",
"protodef-validator": "^1.2.2",
Expand Down
60 changes: 1 addition & 59 deletions src/compiler.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const ClosureCompiler = require('google-closure-compiler').jsCompiler

const numeric = require('./datatypes/numeric')
const utils = require('./datatypes/utils')

Expand Down Expand Up @@ -51,50 +49,6 @@ class ProtoDefCompiler {
const readCtx = this.readCompiler.compile(readCode)
return new CompiledProtodef(sizeOfCtx, writeCtx, readCtx)
}

compileProtoDef (options = { optimize: false, printCode: false, printOptimizedCode: false }) {
let c = this
return new Promise(resolve => {
const sizeOfCode = c.sizeOfCompiler.generate()
const writeCode = c.writeCompiler.generate()
const readCode = c.readCompiler.generate()

if (options.printCode) {
console.log('// SizeOf:')
console.log(sizeOfCode)
console.log('// Write:')
console.log(writeCode)
console.log('// Read:')
console.log(readCode)
}

if (options.optimize) {
optimize(sizeOfCode, (sizeOfCode) => {
optimize(writeCode, (writeCode) => {
optimize(readCode, (readCode) => {
if (options.printOptimizedCode) {
console.log('// SizeOf:')
console.log(sizeOfCode)
console.log('// Write:')
console.log(writeCode)
console.log('// Read:')
console.log(readCode)
}
const sizeOfCtx = c.sizeOfCompiler.compile(sizeOfCode)
const writeCtx = c.writeCompiler.compile(writeCode)
const readCtx = c.readCompiler.compile(readCode)
resolve(new CompiledProtodef(sizeOfCtx, writeCtx, readCtx))
})
})
})
} else {
const sizeOfCtx = c.sizeOfCompiler.compile(sizeOfCode)
const writeCtx = c.writeCompiler.compile(writeCode)
const readCtx = c.readCompiler.compile(readCode)
resolve(new CompiledProtodef(sizeOfCtx, writeCtx, readCtx))
}
})
}
}

class CompiledProtodef {
Expand Down Expand Up @@ -458,21 +412,9 @@ class SizeOfCompiler extends Compiler {
}
}

function optimize (code, cb) {
const closureCompiler = new ClosureCompiler({
compilation_level: 'SIMPLE'
})
closureCompiler.run([{
src: code
}], (exitCode, stdOut, stdErr) => {
cb(stdOut[0].src)
})
}

module.exports = {
ReadCompiler,
WriteCompiler,
SizeOfCompiler,
ProtoDefCompiler,
optimize
ProtoDefCompiler
}

0 comments on commit a8419b9

Please sign in to comment.