Skip to content

Commit

Permalink
Merge pull request #123 from Shopify/pz-instrument-float-bug
Browse files Browse the repository at this point in the history
Instrument float bug
  • Loading branch information
peterzhu2118 authored Nov 6, 2020
2 parents 9cb42ee + da97e3c commit 8a9a049
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 0 deletions.
6 changes: 6 additions & 0 deletions ext/liquid_c/lexer.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "liquid.h"
#include "lexer.h"
#include "usage.h"
#include <stdio.h>

const char *symbol_names[TOKEN_END] = {
Expand Down Expand Up @@ -107,6 +108,11 @@ const char *lex_one(const char *start, const char *end, lexer_token_t *token)
}
}

// Instrument for bug: https://github.com/Shopify/liquid-c/pull/120
if (c == '-' && str + 1 < end && str[1] == '.') {
usage_increment("liquid_c_negative_float_without_integer");
}

if (ISDIGIT(c) || c == '-') {
int has_dot = 0;
cur = str;
Expand Down
2 changes: 2 additions & 0 deletions ext/liquid_c/liquid.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "context.h"
#include "variable_lookup.h"
#include "vm.h"
#include "usage.h"

ID id_evaluate;
ID id_to_liquid;
Expand Down Expand Up @@ -82,5 +83,6 @@ void Init_liquid_c(void)
init_liquid_context();
init_liquid_variable_lookup();
init_liquid_vm();
init_liquid_usage();
}

18 changes: 18 additions & 0 deletions ext/liquid_c/usage.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "usage.h"

static VALUE cLiquidUsage;
static ID id_increment;

void usage_increment(const char *name)
{
VALUE name_str = rb_str_new_cstr(name);
rb_funcall(cLiquidUsage, id_increment, 1, name_str);
}

void init_liquid_usage()
{
cLiquidUsage = rb_const_get(mLiquid, rb_intern("Usage"));
rb_global_variable(&cLiquidUsage);

id_increment = rb_intern("increment");
}
9 changes: 9 additions & 0 deletions ext/liquid_c/usage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef LIQUID_USAGE_H
#define LIQUID_USAGE_H

#include "liquid.h"

void init_liquid_usage();
void usage_increment(const char *name);

#endif
15 changes: 15 additions & 0 deletions test/unit/block_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,21 @@ def test_op_write_raw_w
assert_equal(source, template.render!)
end

# Test for bug: https://github.com/Shopify/liquid-c/pull/120
def test_bug_120_instrument
calls = []
Liquid::Usage.stub(:increment, ->(name) { calls << name }) do
Liquid::Template.parse("{{ -.1 }}")
end
assert_equal(["liquid_c_negative_float_without_integer"], calls)

calls = []
Liquid::Usage.stub(:increment, ->(name) { calls << name }) do
Liquid::Template.parse("{{ .1 }}")
end
assert_equal([], calls)
end

def test_disassemble_raw_w
source = "a" * 2**8
template = Liquid::Template.parse(source)
Expand Down

0 comments on commit 8a9a049

Please sign in to comment.