Skip to content

Commit

Permalink
Move, cleanup and test expand_byte_size_suffixes. NFC. (#11430)
Browse files Browse the repository at this point in the history
I noticed this needed improvement when reviewing #11162
  • Loading branch information
sbc100 authored Jun 17, 2020
1 parent 3a0e729 commit c884129
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 16 deletions.
18 changes: 17 additions & 1 deletion emcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,22 @@ def embed_memfile(options):
not use_source_map(options))))


def expand_byte_size_suffixes(value):
"""Given a string with KB/MB size suffixes, such as "32MB", computes how
many bytes that is and returns it as an integer.
"""
value = value.strip()
match = re.fullmatch(r'(\d+)\s*([kmgt]?b)?', value, re.I)
if not match:
exit_with_error("invalid byte size `%s`. Valid suffixes are: kb, mb, gb, tb" % value)
value, suffix = match.groups()
value = int(value)
if suffix:
size_suffixes = {suffix: 1024 ** i for i, suffix in enumerate(['b', 'kb', 'mb', 'gb', 'tb'])}
value *= size_suffixes[suffix.lower()]
return value


def apply_settings(changes):
"""Take a list of settings in form `NAME=VALUE` and apply them to the global
Settings object.
Expand Down Expand Up @@ -421,7 +437,7 @@ def standardize_setting_change(key, value):
# In those settings fields that represent amount of memory, translate suffixes to multiples of 1024.
if key in ('TOTAL_STACK', 'INITIAL_MEMORY', 'MEMORY_GROWTH_LINEAR_STEP', 'MEMORY_GROWTH_GEOMETRIC_STEP',
'GL_MAX_TEMP_BUFFER_SIZE', 'MAXIMUM_MEMORY', 'DEFAULT_PTHREAD_STACK_SIZE'):
value = str(shared.expand_byte_size_suffixes(value))
value = str(expand_byte_size_suffixes(value))

if value[0] == '@':
filename = value[1:]
Expand Down
7 changes: 7 additions & 0 deletions tests/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -10465,6 +10465,13 @@ def test_xclang_flag(self):
create_test_file('foo.h', ' ')
run_process([EMCC, '-c', '-o', 'out.o', '-Xclang', '-include', '-Xclang', 'foo.h', path_from_root('tests', 'hello_world.c')])

def test_emcc_size_parsing(self):
create_test_file('foo.h', ' ')
err = self.expect_fail([EMCC, '-s', 'TOTAL_MEMORY=X'])
self.assertContained('error: invalid byte size `X`. Valid suffixes are: kb, mb, gb, tb', err)
err = self.expect_fail([EMCC, '-s', 'TOTAL_MEMORY=11PB'])
self.assertContained('error: invalid byte size `11PB`. Valid suffixes are: kb, mb, gb, tb', err)

def test_native_call_before_init(self):
self.set_setting('ASSERTIONS')
self.set_setting('EXPORTED_FUNCTIONS', ['_foo'])
Expand Down
15 changes: 0 additions & 15 deletions tools/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -878,21 +878,6 @@ def get_cflags(user_args, cxx):
return c_opts + emsdk_cflags(user_args, cxx)


def expand_byte_size_suffixes(value):
"""Given a string with KB/MB size suffixes, such as "32MB", computes how
many bytes that is and returns it as an integer.
"""
SIZE_SUFFIXES = {suffix: 1024 ** i for i, suffix in enumerate(['b', 'kb', 'mb', 'gb', 'tb'])}
match = re.match(r'\s*(\d+)\s*([kmgt]?b)$', value, re.I)
if not match:
try:
return int(value)
except ValueError:
raise Exception("Invalid byte size, valid suffixes: KB, MB, GB, TB")
value, suffix = match.groups()
return int(value) * SIZE_SUFFIXES[suffix.lower()]


# Settings. A global singleton. Not pretty, but nicer than passing |, settings| everywhere
class SettingsManager(object):

Expand Down

0 comments on commit c884129

Please sign in to comment.