Skip to content

Commit

Permalink
throw together a quick dependency management system
Browse files Browse the repository at this point in the history
  • Loading branch information
shicks committed Jan 8, 2016
1 parent b219ade commit 2ec8a4a
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 13 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
deps.d
*.compiled.*
*.srcmap
scraps/*
13 changes: 9 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
test4.compiled.js: bufferedaudionode.js stepgeneratornode.js test4.js Makefile
{ java -jar ~/Downloads/compiler.jar --js=bufferedaudionode.js --js=stepgeneratornode.js --js=test4.js --language_in=ES6_STRICT --language_out=ES5_STRICT --create_source_map=test4.srcmap --jscomp_warning=checkTypes --jscomp_warning=checkVars; echo "//# sourceMappingURL=test4.srcmap"; } >| test4.compiled.js
.PHONY: all

nes.compiled.js: clock.js mem.js apu/lengthcounter.js apu/envelope.js apu/pulse.js apu/apu.js cpu.js bankswitcher.js nsf.js stepgeneratornode.js bufferedaudionode.js nsfplayer.js Makefile
{ java -jar ~/Downloads/compiler.jar --js=clock.js --js=mem.js --js=apu/lengthcounter.js --js=apu/envelope.js --js=apu/pulse.js --js=apu/apu.js --js=cpu.js --js=bankswitcher.js --js=nsf.js --js=bufferedaudionode.js --js=stepgeneratornode.js --js=nsfplayer.js --language_in=ES6_STRICT --language_out=ES5_STRICT --create_source_map=nes.srcmap --jscomp_warning=checkTypes --jscomp_warning=checkVars; echo "//# sourceMappingURL=nes.srcmap"; } >| nes.compiled.js
all: nes.compiled.js # test4.compiled.js

JSCOMP=java -jar ~/Downloads/compiler.jar

deps.d: deps.pl
./deps.pl

-include deps.d
8 changes: 4 additions & 4 deletions bufferedaudionode.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export default class BufferedAudioNode {
/** Resets everything to an empty buffer. */
reset() {
if (this.writeResolver_) this.writeResolver_();
this.s_.stop();
this.s_.stop(0);
this.s_.disconnect();

this.s_ = this.ac_.createBufferSource();
Expand Down Expand Up @@ -117,8 +117,8 @@ export default class BufferedAudioNode {


/**
* @param {!ArrayLike<number>} left
* @param {!ArrayLike<number>=} right
* @param {!Array<number>|!Float32Array} left
* @param {!Array<number>|!Float32Array=} right
* @param {number=} offset
* @param {!Promise=} promise
* @return {!Promise} Promise that completes when buffer written.
Expand All @@ -137,7 +137,7 @@ export default class BufferedAudioNode {
let pos = this.written_ % this.fc_;
if (offset > end) throw new Error('impossible ' + offset + ' > ' + end);
if (this.written_ == 0 && offset < end) {
this.s_.start();
this.s_.start(this.ac_.currentTime);
this.started_ = this.ac_.currentTime * this.ac_.sampleRate;
}
for (let i = offset; i < end; i++) {
Expand Down
105 changes: 105 additions & 0 deletions deps.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#!/usr/bin/perl

# Dependency and makefile management.
# Usage: ./deps.pl
# Result: Generates a makefile based on in-file markup.

# Input files:
# foo.js:
# //@ foo.compiled.js --checkTypes=warning
# import Whatever from './bar';
# bar.js:
# import Something from './baz';
#
# Performs a toposort, collecting all deps, and generates deps.d:
#
# foo.compiled.js: foo.js bar.js deps.d
# $(JSCOMP) ... --js=foo.js --js=bar.js --js_output_file=foo.compiled.js --checkTypes=warning
# deps.d: foo.js bar.js
#
# This should be included in the top-level Makefile as:
#
# .PHONY: all
# all: foo.compiled.js
#
# deps.d:
# ./deps.pl

use strict;
use warnings;

use File::Basename qw/dirname/;

# Find all files recursively from current dir
open FIND, "find . -name '*.js' | sed 's+^./++' |";
my @files = <FIND>;
close FIND;
map {chomp $_} @files;

# Scan all the files
my %generated = ();
my %deps = ();
foreach my $file (@files) {
$deps{$file} = '';
open JS, $file;
while (<JS>) {
if (m|//@\s*(\S+)\s+(.*)|) {
$generated{$1} = "$file $2";
} elsif (m|import .* from '(\.\.?/[^']+)'|) {
my $dep = $1;
my $dir = dirname $file;
while ($dep =~ s|^\./|| or $dep =~ s|^\.\./||) {
$dir = dirname $dir if $& eq '../';
}
$dep = "$dir/$dep.js";
$dep =~ s|^(?:\./)+||;
$deps{$file} .= " $dep";
}
}
close JS;
}

my $makefile = '';

# Now build up the makefile.
foreach my $out (sort(keys(%generated))) {
delete $deps{$out};
# Find all the deps
my ($file, $flags) = split / /, $generated{$out}, 2;
my %d = ($file => 1);
my @q = ($file);
while (@q) {
my $cur = shift @q;
foreach (split / /, $deps{$cur}) {
next unless $_;
next if defined $d{$_};
$d{$_} = 1;
push @q, $_;
}
}
# Generate the makefile line
my $srcmap = $out;
$srcmap =~ s/\.js$//;
$srcmap .= ".srcmap";
my @deps = sort(keys(%d));
my $header = "$out: @deps deps.d";
my $cmd = "{ \$(JSCOMP) $flags";
foreach (@deps) { $cmd .= " --js=$_"; }
$cmd .= " --create_source_map=$srcmap; echo '//# sourceMappingURL=$srcmap'; } >| $out";
$makefile .= "$header\n\t$cmd\n\n";
}

my @srcs = sort(keys(%deps));
$makefile .= "deps.d: @srcs\n";

# Now read the existing file if it's there and only update if changed.
if (open DEPS, "deps.d") {
$/ = undef;
my $prev = <DEPS>;
close DEPS;
exit 0 if $prev eq $makefile;
}

open DEPS, ">deps.d";
print DEPS $makefile;
close DEPS;
2 changes: 2 additions & 0 deletions nsfplayer.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//@ nes.compiled.js --language_in=ES6_STRICT --language_out=ES5_STRICT --create_source_map=nes.srcmap --jscomp_warning=checkTypes --jscomp_warning=checkVars

import Apu from './apu/apu';
import BankSwitcher from './bankswitcher';
import Clock from './clock';
Expand Down
10 changes: 5 additions & 5 deletions stepgeneratornode.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export default class StepGeneratorNode {
if (endSample > this.sample_) {
const deltas =
this.kernel_.convolve(this.sample_, endSample, this.steps_)
for (let delta of deltas) {
for (let delta of /** @type {!Iterable<number>} */ (deltas)) {
// TODO(sdh): can we remove the floating-point error drift?!?
//this.lastSample_ *= 0.9999995;
samples.push(this.lastSample_ += delta);
Expand Down Expand Up @@ -247,10 +247,10 @@ function cisi(x) {

const t = Math.abs(x);
let cs; // complex result
if (!t) return new Complex(-BIG, 0);
if (!t) return new Complex(-BIG);
if (t > TMIN) {
let b = new Complex(1, t);
let c = new Complex(BIG, 0);
let c = new Complex(BIG);
let d = b.inv();
let h = d;
let i;
Expand Down Expand Up @@ -302,9 +302,9 @@ function cisi(x) {
}

class Complex {
constructor(re, im) {
constructor(re, im = 0) {
this.re = re;
this.im = im || 0;
this.im = im;
Object.freeze(this);
}

Expand Down
2 changes: 2 additions & 0 deletions test4.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//@ test4.compiled.js --language_in=ES6_STRICT --language_out=ES5_STRICT --create_source_map=nes.srcmap --jscomp_warning=checkTypes --jscomp_warning=checkVars

'use strict';

import StepGeneratorNode from './stepgeneratornode';
Expand Down

0 comments on commit 2ec8a4a

Please sign in to comment.