Skip to content

Commit

Permalink
Fix #266. Bug in closures.
Browse files Browse the repository at this point in the history
  • Loading branch information
dim-s committed Jun 14, 2018
1 parent bd59731 commit 3e178bc
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 6 deletions.
2 changes: 1 addition & 1 deletion jphp-core/package.php.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

name: jphp-core
version: 1.0.3
version: 1.0.4
description: Compiler and Launcher for JPHP.

deps:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1548,7 +1548,7 @@ protected void writeDefineVariable(VariableExprToken value) {
variable.setValue(null);
variable.setReference(true);
} else { // simple local variables
if (variable.isReference()) {
if (variable.isReference() || methodStatement.variable(value).isArrayAccess()) {
writePushNewObject(ReferenceMemory.class);
} else {
writePushNull();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.develnext.jphp.core.compiler.jvm.misc.JumpItem;
import org.develnext.jphp.core.compiler.jvm.misc.LocalVariable;
import org.develnext.jphp.core.compiler.jvm.node.MethodNodeImpl;
import org.develnext.jphp.core.syntax.VariableStats;
import org.develnext.jphp.core.tokenizer.TokenMeta;
import org.develnext.jphp.core.tokenizer.token.expr.value.NameToken;
import org.develnext.jphp.core.tokenizer.token.expr.value.StaticAccessExprToken;
Expand Down Expand Up @@ -339,7 +340,9 @@ void writeHeader(){

for (ArgumentStmtToken argument : statement.getUses()) {
LocalVariable local;
if (statement.isDynamicLocal()) {
VariableStats variableStats = statement.variable(argument.getName());

if (statement.isDynamicLocal() || variableStats.isArrayAccess()) {
expressionCompiler.writeDefineVariable(argument.getName());
local = getLocalVariable(argument.getName().getName());
} else {
Expand All @@ -348,13 +351,13 @@ void writeHeader(){

if (argument.isReference()){
local.setReference(true);
statement.variable(argument.getName()).setUnstable(true);
variableStats.setUnstable(true);
}

expressionCompiler.writePushDup();
expressionCompiler.writePushGetFromArray(i, Memory.class);

if (statement.isDynamicLocal()) {
if (statement.isDynamicLocal() || variableStats.isArrayAccess()) {
expressionCompiler.writeVarAssign(local, argument.getName(), false, false);
} else {
expressionCompiler.writeVarStore(local, false, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,9 @@ public void testStaticVars(){
public void testBugs() {
check("closures/bug138.php");
}

@Test
public void testBug266() {
check("closures/bug266.php");
}
}
20 changes: 20 additions & 0 deletions jphp-core/tests/resources/closures/bug266.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
Bug #266
--FILE--
<?php

function test() {
$data = null;

$fn = function () use ($data) {
$data['x'] = 'ddd';
var_dump("success");
};

$fn();
}

test();
?>
--EXPECT--
string(7) "success"
11 changes: 10 additions & 1 deletion packager/buildSrc/GradlePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use packager\Vendor;
use php\lang\Process;
use php\lang\System;
use php\lib\arr;
use php\lib\fs;
use php\lib\str;
use text\TextWord;
Expand Down Expand Up @@ -87,6 +88,13 @@ protected function makeGradleBuild(Package $pkg)
$provided[] = " provided files('" . str::join($deps, "',\r\n '") . "')";
}

$addRepos = [];
if ($this->config['repos']) {
$addRepos = flow($this->config['repos'])->map(function ($repo) {
return " maven { url '$repo' }";
})->toArray();
}

$lines = [
'apply plugin: "java"',
'apply plugin: "idea"',
Expand All @@ -104,6 +112,7 @@ protected function makeGradleBuild(Package $pkg)
" mavenCentral()",
" jcenter()",
" maven { url 'https://oss.sonatype.org/content/groups/public' }",
$addRepos,
"}",
"",
"configurations { provided }",
Expand All @@ -127,7 +136,7 @@ protected function makeGradleBuild(Package $pkg)
"}"
];

Tasks::createFile("./build.gradle", str::join($lines, "\r\n"));
Tasks::createFile("./build.gradle", str::join(arr::flatten($lines), "\r\n"));
}

/**
Expand Down

0 comments on commit 3e178bc

Please sign in to comment.